Partitech Sonata Extra Bundle : AsAdmin() attribute

SonataExtra AsAdmin() attribute

Thanks to php attributes; exit the old 15kms long services.yaml config file with a quick per class configuration.

old type was :

       class: App\Admin\MyEntityAdmin
       arguments: [~, App\Entity\MyEntity, ~]
           - { name: sonata.admin, manager_type: orm, label: "My Entity Admin" }
           - [ setTranslationDomain, [partitech]]

Thanks to autowiring "Arguments" is no more needed.

here is a basic AsAdmin() configuration :

use Partitech\SonataExtra\Attribute\AsAdmin;
use Symfony\Contracts\Service\Attribute\Required;

    manager_type: 'orm',
    group: 'Admin',
    label: 'My Entity Admin',
    model_class: \Partitech\SonataExtra\Entity\MyEntity::class,
    controller: \Partitech\SonataExtra\Controller\Admin\MyEntityController::class,
    calls: [
        ['setTranslationDomain', ['partitech']],
class MyEntityAdmin extends AbstractAdmin
    private MyRequiredService $myRequiredService;
    private MyAttributeCallService $myAttributeCallService;
    public function setMyRequeredService(MyRequiredService $myRequiredService): void
        $this->myRequiredService = $myRequiredService;

    public function setMyAttributeCallService(MyAttributeCallService $myAttributeCallService): void
        $this->myAttributeCallService = $myAttributeCallService;

If you need some more services in your Admin class, user the #[required] method annotation to mirror the construct autowiring, or set calls in the AsAdmin() attribute.

Be aware that your Admins Classes have to be in the Services registration in your yaml file s either

  • globally like :
        autowire: true
        autoconfigure: true

        resource: '../src/'
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

Your src/Admin/ directory must not be exclude.

  • one line Service declaration :

    App\SecondAdminDirectory\MyGreatAdmin: ~

If you use fancy automatic declaration based on Instance like this, this will not work:

    autowire: true
    autoconfigure: true
      tags: { name: sonata.admin }