【问题标题】:Sonata Admin Bundle - Form type: sonata_type_collection - custom template?Sonata Admin Bundle - 表单类型:sonata_type_collection - 自定义模板?
【发布时间】:2012-07-15 02:33:40
【问题描述】:

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

我尝试过这些方法:

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

但无济于事。

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

有人知道这是否可行吗?

谢谢

【问题讨论】:

    标签: templates symfony symfony-sonata sonata-admin


    【解决方案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 的块,它只适用于这个管理表单:)

    为了在我的管理类中完成这个解决方案,我添加了这个函数:

    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 %}
    

    【讨论】:

    • 只要完成你的回答,你就可以用twig注册你的模板文件,这样你就不需要将管理类中的术语与“getFormTheme()”合并,见:symfony.com/doc/current/cookbook/form/…
    • 在那里你提到“定义一个名为...的黑色”与在 admin.slides.html.twig 中创建块的最后一步相同?或者你在哪里定义那个块?
    猜你喜欢
    • 2018-07-22
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2013-12-19
    • 2016-06-25
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多