dsdsm2016 2015-10-20 22:48
浏览 20
已采纳

如何动态修改sonata_type_collection的子表单?

I have a field of 'sonata_type_collection' in a form which is defined in a Sonata Admin class. I need to modify the children of that form based on child's position. In my particular case, each row in the collection has a 'Delete' checkbox, and I'd like to disable that checkbox only for the first element of the collection. Any idea how to achieve this?

  • 写回答

1条回答 默认 最新

  • dongshi1914 2015-10-22 09:25
    关注

    The only way that I found is by overriding the form_admin_fields.html.twig and add your own blocks.

    You can override the template by modifying the related configuration file : https://sonata-project.org/bundles/doctrine-orm-admin/2-2/doc/reference/configuration.html#full-configuration-options or use the SonataEasyExtendsBundle to extend SonataDoctrineOrmBundle.

    You have to create 2 block one for you collection and one for your relation type (OneToMany or ManyToMany).

    The annoying part is to find the name of your block, it's formed by your admin service name + field name + 'sonata_type_collection_widget'.

    It depends of your Sonata version but here is a collection block example that I use :

    {% block sonata_admin_challenge_organizers_sonata_type_collection_widget %}
        {% if sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::ONE_TO_MANY') %}
            {{ block('sonata_admin_challenge_organizers_orm_one_to_many_widget') }}
        {% elseif sonata_admin.field_description.mappingtype == constant('Doctrine\\ORM\\Mapping\\ClassMetadataInfo::MANY_TO_MANY') %}
            {{ block('sonata_admin_orm_many_to_many_widget') }}
        {% else %}
            INVALID MODE : {{ id }} - type : sonata_type_collection - mapping : {{ sonata_admin.field_description.mappingtype }}
        {% endif %}
    {% endblock %}
    

    Once your collection block is done you have to add a new block for the oneToMany or ManyToMany, you simply copy the template used in your Sonata version and customize it to your need : https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/master/Resources/views/CRUD/edit_orm_one_to_many.html.twig

    In your case, you simply have to add an if statement based on the loop.index value to display or not the delete field : https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/master/Resources/views/CRUD/edit_orm_one_to_many.html.twig#L26.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)