Sonata Admin Bundle - 表单类型:sonata_type_collection - 自定义模板?

lop*_*ded 8 templates symfony symfony-sonata sonata-admin

是否可以覆盖表单类型的模板:"sonata_type_collection"?

我试过这些方法:

$formMapper->add('slides', 'sonata_type_collection', array(), array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable'  => 'priority',
                'template' => 'MyBundle:Form:slides.admin.html.twig'
            ));
Run Code Online (Sandbox Code Playgroud)

但无济于事.

我知道我可以覆盖整个模板,但我只想为这个表单做这个,而不是我使用这个表单类型的所有地方.

有谁知道这是否可能?

谢谢

lop*_*ded 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);
Run Code Online (Sandbox Code Playgroud)

因此我所要做的就是定义一个名为mycompany_admin_content_galleries_sonata_type_collection_widgetor 的块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')
    );
}
Run Code Online (Sandbox Code Playgroud)

我创建了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 %}
Run Code Online (Sandbox Code Playgroud)