Search code examples
phpsymfonyeasyadmin

Create new list field type for EasyAdmin


With EasyAdmin Symfony bundle, with Symfony 4.2, how create a new list field type?

Use case

"I want to display a link to show page in the list table"

(not a form type, a list type):

easy_admin:
  entities:
    offer:
      class: App\Entity\Offer
      list:
        fields:
          - { property: name, type: MY_TYPE??? }

Solution

  • You have 2 solutions i believe :

    1. If the url is stored in your object there is a custom type for this : https://symfony.com/doc/2.x/bundles/EasyAdminBundle/book/list-search-show-configuration.html#url-data-type

    It allows you to display an url :

    # config/packages/easy_admin.yaml
    easy_admin:
        entities:
            Product:
                class: App\Entity\User
                list:
                    fields:
                        - { property: 'blogUrl', type: 'url' }
    
    1. If you don't have the full url you can try by using a custom template : https://symfony.com/doc/2.x/bundles/EasyAdminBundle/tutorials/custom-property-options.html#custom-property-options

    This way you can define a custom template to generate your url and pass a param if you need :

    # config/packages/easy_admin.yaml
    easy_admin:
        entities:
            Product:
                class: App\Entity\Product
                list:
                    fields:
                        # ...
                        - { property: 'tags', template: 'admin/tag_collection.html.twig',
                            label_colors: ['primary', 'success', 'info'] }