Sonataadminbundle: Custom template for sonata_type_collection

Created on 11 Jan 2013  路  19Comments  路  Source: sonata-project/SonataAdminBundle

Hi All!
In my project I want override template for sonata_type_collection, but I want do this for only one form, not for all project.

Any idea?

Example:

      ->add('field', 'sonata_type_collection', array('required'     => false,
                                                                     'label'        => 'Some Label',
                                                                     'by_reference' => false,), array(
                                                                                                                  'edit'            => 'inline',
                                                                                                                  'inline'          => 'table',
                                                                                                                  'template' => 'Some Template'))

Doesn't work.

Most helpful comment

I find solution. In my admin class i override getFormTheme method like this:

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

Then, I create admin.theme.html.twig template, and in this template i wrote:

{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}

{% block sonata_admin_orm_one_to_many_widget %}
Override sonata_type_collection
{% endblock %}

In {% block sonata_admin_orm_one_to_many_widget %} i override all, what i want for sonata_type_collection field.

All 19 comments

I find solution. In my admin class i override getFormTheme method like this:

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

Then, I create admin.theme.html.twig template, and in this template i wrote:

{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}

{% block sonata_admin_orm_one_to_many_widget %}
Override sonata_type_collection
{% endblock %}

In {% block sonata_admin_orm_one_to_many_widget %} i override all, what i want for sonata_type_collection field.

@qwebot Thanks a lot for posting your solution - this really helped me move forward with my problem. My admin form has more than one sonata_type_collection. The above overrides all collections in the form with the same template. You wouldn't happen to know how to override a 'specific' collection, and leave the others intact?

OK... if you have a form that has multiple sonata_type_collections, and you want to override each collection differently (or display the default template for one or more of them), you can do this:

//following on from example above, consider the following to be admin.theme.html.twig

{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}

{% block sonata_admin_orm_one_to_many_widget %}

{% if sonata_admin.name == 'myAdminClassNameHere' %}
    {% include 'MyBundle:myAdminClass:myCustomTemplate.html.twig' %}
{%  else %}
    {% include 'SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig' %}
{% endif %}

{% endblock %}

Thank you @mariosanchez - that is my blog! I'm very happy if it helped you!

I tried everything above and even in the blog with no success. I have created a custom form_admin_fields.html.twig template and saved in in MyBundle\Resources\views\Form\form_admin_fields.html.twig and called it as below but still get Unable to find template "MultimediaBundle:Form:form_admin_fields.twig" in SonataAdminBundle::standard_layout.html.twig at line 11 error

public function getFormTheme()
{
return array_merge(
parent::getFormTheme(),
array('MultimediaBundle:Form:form_admin_fields.html.twig')
);
}

Thanks @Tocacar for the article. Just wondering though, how did you come to know this?

@florinutz You're welcome! I just pecked away at it until I figured it out.

@marvoh I think you simply missed the html in your filename:
"MultimediaBundle:Form:form_admin_fields.twig" != MultimediaBundle:Form:form_admin_fields.html.twig

BTW: Isn't this actually a SonataDoctrineORMAdminBundle issue?

I am trying to override the Javascripts inside the templates but having problems with the uniqid:
https://github.com/sonata-project/SonataDoctrineORMAdminBundle/issues/378

Has anybody else dealed with this use case?

BTW: Using the solution provided by @Tocacar I had to remove the following line in my template:

{#{% use 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}#}

Otherwise I got the error:
Template cannot be used as a trait in SonataAdminBundle:CRUD:edit.html.twig at line 36

Maybe a problem with an older PHP/Twig/Symfony version or similar to this issue: https://github.com/sonata-project/SonataAdminBundle/issues/620

Try replacing use with extends, that worked for me.
{% extends 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}

Hello everyone, i almost got it, but i have an issue, how can i override the {% block sonata_admin_orm_one_to_many_widget %} if my service is:

services:
per_scholas_admin.admin.register_request:

and my sonata_type_collection is:

        $formMapper
                ->add('dynamic_data_options', 'sonata_type_collection', array(
                    'by_reference' => false,
                    'cascade_validation' => true,
                    'required' => false,
                        ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                ))

I'll be very grateful with your help.

In your Admin class @odeg36 :

    public function getFormTheme()
    {
        return array_merge(
            parent::getFormTheme(),
            array('AppBundle:PerScholasAdmin:form_admin_fields.html.twig')
        );
    }

The add a template to /Resources/views/PerScholarAdmin/form_admin_fields.html.twig:

{% extends 'SonataAdminBundle:Form:form_admin_fields.html.twig' %}

{% block sonata_admin_orm_one_to_many_widget %}
    {% include 'AppBundle:PerScholarAdmin:edit_orm_one_to_many.html.twig' %}    
{% endblock %}

and another template for edit_orm_one_to_many.html.twig.

Should be we add this "special use case" to the docs, @rande ?

BTW: If your programming in an english language, why not call it PerSchoolAdmin? Who knows who will be your developer follow-up? ;)

@webdevilopers Thank you very much! but does it help me to use it for different admins? And for the english language, the name of the project is PerScholas , we can't change that :P

The solution provided canbe done for each Admin - not globally.

Works great! but i cant figured out why you create a new block. anyway thank you very much!!

Greetings from Colombia :+1:

Which block is new?

thanks!

Is there by any chance a less hacky way to do this now ?

Was this page helpful?
0 / 5 - 0 ratings