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 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?