代码之家  ›  专栏  ›  技术社区  ›  Thomas Decaux

为EasyAdmin创建新的列表字段类型

  •  0
  • Thomas Decaux  · 技术社区  · 6 年前

    对于EasyAdmin Symfony bundle和Symfony 4.2,如何创建一个新的列表字段类型?

    用例

    “我要在列表表中显示页面的链接”

    (不是表单类型,而是列表类型):

    easy_admin:
      entities:
        offer:
          class: App\Entity\Offer
          list:
            fields:
              - { property: name, type: MY_TYPE??? }
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Snroki    6 年前

    我相信你有两个解决方案:

    1) 如果url存储在对象中,则有一个自定义类型: https://symfony.com/doc/master/bundles/EasyAdminBundle/book/list-search-show-configuration.html#url-data-type

    它允许您显示url:

    # config/packages/easy_admin.yaml
    easy_admin:
        entities:
            Product:
                class: App\Entity\User
                list:
                    fields:
                        - { property: 'blogUrl', type: 'url' }
    

    2) 如果没有完整的url,可以使用自定义模板尝试: https://symfony.com/doc/master/bundles/EasyAdminBundle/tutorials/custom-property-options.html#using-custom-property-options-in-templates

    通过这种方式,您可以定义自定义模板来生成url,并在需要时传递参数:

    # 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'] }