Sonata Admin Bundle - Form type: sonata_type_collection - custom template?

牧云@^-^@ 提交于 2019-12-02 18:53:46

I found a great bit of code in /vendor/sonata-project/admin-bundle/Sonata/AdminBundle/Form/Extension/Field/Type/FormTypeFieldExtension.php which actually sets up an array of types to attach to the form view which it uses to prioritise twig block rendering: (lines 99 to 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);

Therefore all I had to do was define a block called mycompany_admin_content_galleries_sonata_type_collection_widget or mycompany_admin_content_galleries_slides_sonata_type_collection_widget and it only applies to this admin form :)

To complete this solution in my Admin class I added this function:

public function getFormTheme()
{
    return array_merge(
        parent::getFormTheme(),
        array('MyBundle:Gallery:admin.slides.html.twig')
    );
}

and I created MyBundle/Resources/views/Gallery/admin.slides.html.twig, containing the following:

{% 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 %}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!