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.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)