[Symfony] Use references in YAML in EasyAdmin bundle config for virtual entities

Published on 2018-10-23 • Modified on 2018-10-23

One of the many interesting feature of the EasyAdminBundle is that you can define filters for your entities. It allows to have several entries for the same entity but for different types for example. I wanted to share all the configuration of the main entity (in this case Token with all other virtual entities. (in this case TokenType2) without to have to copy/paste all the configuration. I never used YAML references before and this is a perfect use case for them. So here is the code it is self explainatory. The TokenType2 virtual entity will use the three configurations YAML references declared in the main Token entity section.

    # ...
        Token: # ---------------------------------------------------------------
            class: AppBundle\Entity\Token
                actions: &token.list.actions # <------- 1st reference declaration
                    - { name: 'tokenShowPublic', icon: 'binoculars', label: 'View' }
                    # ... long list of actions
                fields: &token.list.fields # <------- 2nd reference declaration
                    - { property: 'id' }
                    # ... long list of fields
                fields: &token.form.fields # <------- 3rd reference declaration
                    - { type: 'group', label: 'IDs', icon: 'gear' }
                    - { property: 'id', type: 'integer', type_options: { disabled: true }, css_class: 'col-sm-2'}
                    # ... long list of fields

            class: AppBundle\Entity\Token
            dql_filter: 'entity.tokenType=2'
                actions: *token.list.actions # <------- use 1st reference
                fields: *token.list.fields # <------- use 2nd reference
                fields: *token.form.fields # <------- use 3rd reference

            # That's it, no more duplicated configuration!

 More on Stackoverflow