Search code examples
symfonysonata-adminsonata

How do you get an Entity's Admin service with Sonata?


Let's say I make a query for a bunch of different mixed entities. Pretend all these entities are in a single inheritance table, and there are a dozen different types.

I want to generate a link to the Sonata edit page for any of those entities.

I can get the fully-qualified class name for the Entity object using the get_class function. I can't figure out a way to give that class to Sonata and have it return the service object or the service id of the Admin representing that class.

If I can get that, I can use the generateUrl method of the Admin class to create the link to the edit form.


Solution

  • All the admin services are stored in a pool.

    Github : https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Admin/Pool.php

    This pool is accessible via a service sonata.admin.pool .

    Your magic method is inside this service:

    $admin = $this->get('sonata.admin.pool')->getAdminByClass('YOUR CLASS NAME');