Extensions

Version: current
  • Maintained versions
Edit this page

Extensions

Admin extensions allow you to add or change features of one or more Admin instances. To create an extension your class must implement the interface Sonata\AdminBundle\Admin\AdminExtensionInterface and be registered as a service. The interface defines a number of functions which you can use to customize the edit form, list view, form validation, alter newly created objects and other admin features:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

final class PublishStatusAdminExtension extends AbstractAdminExtension
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form
            ->add('status', ChoiceType::class, [
                'choices' => [
                    'draft' => 'Draft',
                    'published' => 'Published',
                ],
            ])
        ;
    }
}

Configuration

There are two ways to configure your extensions and connect them to an admin.

You can include this information in the service definition of your extension. Add the tag sonata.admin.extension and use the target attribute to point to the admin you want to modify. Please note you can specify as many tags you want. Set the global attribute to true and the extension will be added to all admins. The priority attribute is 0 by default and can be a positive or negative integer. The higher the priority, the earlier it's executed.

  • YAML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# config/services.yaml

services:
    app.publish.extension:
        class: App\Admin\Extension\PublishStatusAdminExtension
        tags:
            - { name: sonata.admin.extension, target: app.admin.article }
            - { name: sonata.admin.extension, target: app.admin.blog }

    app.order.extension:
        class: App\Admin\Extension\OrderAdminExtension
        tags:
            - { name: sonata.admin.extension, global: true }

    app.important.extension:
        class: App\Admin\Extension\ImportantAdminExtension
        tags:
            - { name: sonata.admin.extension, priority: 5 }

The second option is to add it to your config.

  • YAML
1
2
3
4
5
6
7
# config/packages/sonata_admin.yaml

sonata_admin:
    extensions:
        app.publish.extension:
            admins:
                - app.admin.article

Using config/packages/sonata_admin.yaml file has some advantages, it allows you to keep your configuration centralized and it provides some extra options you can use to wire your extensions in a more dynamic way. This means you can change the behavior of all admins that manage a class of a specific type.

global:
adds the extension to all admins.
admins:
specify one or more admin service ids to which the Extension should be added
excludes:
specify one or more admin service ids to which the Extension should not be added (this will prevent it matching any of the other settings)
extends:
specify one or more classes. If the managed class of an admin extends one of the specified classes the extension will be added to that admin.
implements:
specify one or more interfaces. If the managed class of an admin implements one of the specified interfaces the extension will be added to that admin.
instanceof:
specify one or more classes. If the managed class of an admin extends one of the specified classes or is an instance of that class the extension will be added to that admin.
uses:
Specify one or more traits. If the managed class of an admin uses one of the specified traits the extension will be added to that admin.
priority:
Can be a positive or negative integer. The higher the priority, the earlier it’s executed.
  • YAML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# config/packages/sonata_admin.yaml

sonata_admin:
    extensions:
        app.publish.extension:
            global: true
            admins:
                - app.admin.article
            implements:
                - App\Publish\PublishStatusInterface
            excludes:
                - app.admin.blog
                - app.admin.news
            extends:
                - App\Document\Blog
            instanceof:
                - App\Document\Page
            uses:
                - App\Trait\Timestampable
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.