是否可以覆盖以下表单类型的模板:“sonata_type_collection”?
香港专业教育学院试图遵循以下思路:
$formMapper->add('slides', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'priority',
'template' => 'MyBundle:Form:slides.admin.html.twig'
));
但无济于事。
我知道我可以覆盖整个模板,但是我只想为这种形式做,而不是在所有我使用这种形式类型的地方做。
有人知道这是否可能吗?
谢谢
最佳答案
我在/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type/FormTypeFieldExtension.php
中找到了很多代码,它实际上设置了一个类型数组以附加到窗体 View ,该窗体用于优先处理 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 %}
关于templates - Sonata管理员 bundle -表单类型: sonata_type_collection - custom template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501022/