-->

索纳塔管理软件包 - 表格类型:sonata_type_collection - 自定义模板?(S

2019-07-30 11:11发布

是否有可能来覆盖表单类型的模板:“sonata_type_collection”?

伊夫沿着这些路线的尝试:

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

但无济于事。

我知道我可以覆盖整个模板,但我只想做这种形式,并不是所有的地方我用这种形式类型的地方。

有谁知道这是否可能?

谢谢

Answer 1:

我发现的在代码大比特/vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type/FormTypeFieldExtension.php实际上设置的类型的数组附加到表格视图,它使用优先树枝块渲染:(行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_widgetmycompany_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 %}


文章来源: Sonata Admin Bundle - Form type: sonata_type_collection - custom template?