是否可以覆盖表单类型的模板:“sonata_type_collection”?
我试过这些方法:
$formMapper->add('slides', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'priority',
'template' => 'MyBundle:Form:slides.admin.html.twig'
));
但无济于事。
我知道我可以覆盖整个模板,但我只想为这个表单执行此操作,而不是我使用此表单类型的所有地方。
有人知道这是否可行?
由于
答案 0 :(得分:18)
我在/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type/FormTypeFieldExtension.php
中找到了很多代码,它实际上设置了一个类型数组,用于附加到窗体视图,它用于优先化twig块渲染:(第99到105行)
// add a new block types, so the Admin Form element can be tweaked based on the admin code
$types = $view->getVar('types');
$baseName = str_replace('.', '_', $sonataAdmin['field_description']->getAdmin()->getCode());
$baseType = $types[count($types) - 1];
$types[] = sprintf('%s_%s', $baseName, $baseType);
$types[] = sprintf('%s_%s_%s', $baseName, $sonataAdmin['field_description']->getName(), $baseType);
因此,我所要做的就是定义一个名为mycompany_admin_content_galleries_sonata_type_collection_widget
或mycompany_admin_content_galleries_slides_sonata_type_collection_widget
的块,它只适用于此管理员表单:)
要在我的Admin类中完成此解决方案,我添加了此功能:
public function getFormTheme()
{
return array_merge(
parent::getFormTheme(),
array('MyBundle:Gallery:admin.slides.html.twig')
);
}
我创建了MyBundle/Resources/views/Gallery/admin.slides.html.twig
,其中包含以下内容:
{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %} // I think this
line is not really needed as the base admin's form theme uses this file
{% block my_bundle_content_pages_slides_sonata_type_collection_widget %}
// copied and edited the contents of Sonata/DoctrineORMAdminBundle/Resources/views/CRUD/edit_orm_one_to_many.html.twig
{% endblock %}