templates - Sonata管理员 bundle -表单类型: sonata_type_collection - custom template?

标签 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'
            ));

但无济于事。

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

有人知道这是否可能吗?

谢谢

最佳答案

我在/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_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 %}

关于templates - Sonata管理员 bundle -表单类型: sonata_type_collection - custom template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501022/

相关文章:

c# - Xamarin 表单中每张幻灯片使用不同模板的 CarouselView

c++ - 静态函数模板和 MISRA C++

c++ - 使用模板化类的成员类型作为类成员变量的类型

c++ - 类模板构造函数中引用的未解析外部符号(在 VS 2008 中)

symfony - 看来学说监听者没有被解雇

symfony - 如何在 Doctrine 2 中设置日期?

symfony - Sonata Block Bundle 中的上下文是什么

symfony - 如何在 SonataAdmin 中创建自定义 DataGrid 过滤器

php - 如何使用 SonataMediaBundle 在 Twig 中获取图像路径?

ruby - Capistrano 3 权限未在缓存和版本上正确设置