Sonata Admin Bundle - 表单类型:sonata_type_collection - 自定义模板? [英] Sonata Admin Bundle - Form type: sonata_type_collection - custom template?
问题描述
是否可以覆盖表单类型的模板:sonata_type_collection"?
Is it possible to override the template for the form type: "sonata_type_collection"?
我已经尝试过:
$formMapper->add('slides', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'priority',
'template' => 'MyBundle:Form:slides.admin.html.twig'
));
但无济于事.
我知道我可以覆盖整个模板,但我只想为这个表单做这个,而不是我使用这个表单类型的所有地方.
I know I could override the entire template, but I only want to do it for this form, not all the places where I use this form type.
有人知道这是否可能吗?
Does anyone know if this is possible?
谢谢
推荐答案
我在 /vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type 中发现了大量代码/FormTypeFieldExtension.php
它实际上设置了一个类型数组以附加到它用来优先处理树枝块渲染的表单视图:(第 99 到 105 行)
I found a great bit of code in /vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type/FormTypeFieldExtension.php
which actually sets up an array of types to attach to the form view which it uses to prioritise twig block rendering: (lines 99 to 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
的块,它仅适用于此管理表单 :)
Therefore all I had to do was define a block called mycompany_admin_content_galleries_sonata_type_collection_widget
or mycompany_admin_content_galleries_slides_sonata_type_collection_widget
and it only applies to this admin form :)
为了在我的管理类中完成这个解决方案,我添加了这个函数:
To complete this solution in my Admin class I added this function:
public function getFormTheme()
{
return array_merge(
parent::getFormTheme(),
array('MyBundle:Gallery:admin.slides.html.twig')
);
}
然后我创建了 MyBundle/Resources/views/Gallery/admin.slides.html.twig
,包含以下内容:
and I created MyBundle/Resources/views/Gallery/admin.slides.html.twig
, containing the following:
{% 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 %}
这篇关于Sonata Admin Bundle - 表单类型:sonata_type_collection - 自定义模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!