lopsided
lopsided

Reputation: 2410

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

Is it possible to override the template for the form type: "sonata_type_collection"?

Ive tried along these lines:

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

but to no avail.

I know I could override the entire template, but I only want to do it for this form, not all the places where I use this form type.

Does anyone know if this is possible?

Thanks

Upvotes: 8

Views: 12145

Answers (2)

Oriol Planas
Oriol Planas

Reputation: 107

You can configure it like this:

               ->add('informacions', CollectionType::class, array(
                    'type_options' => array(
                        'by_reference' => true,
                        'delete' => true,
                        'delete_options' => array(
                            'type'         => CheckboxType::class,
                            'type_options' => array(
                                'mapped'   => false,
                                'required' => false,
                            )
                        )
                    )), array(
                        'edit' => 'inline',
                        'inline' => 'custom',
                        'sortable' => 'position',
                ))

The inline parameter can be set as a table or whatsoever. If you choose table, it displays this template edit_one_to_many_inline_table.html; otherwise, if you choose something different, it displays edit_one_to_many_inline_tabs.html

As you can check out in CRUD/Association/edit_one_to_many.html.twig:

            {% if sonata_admin.inline == 'table' %}
                {% if form.children|length > 0 %}
                    {% include '@SonataAdmin/CRUD/Association/edit_one_to_many_inline_table.html.twig' %}
                {% endif %}
            {% elseif form.children|length > 0 %}
                {% set associationAdmin = sonata_admin.field_description.associationadmin %}
                {% include '@SonataAdmin/CRUD/Association/edit_one_to_many_inline_tabs.html.twig' %}

            {% endif %}

The key is to override edit_one_to_many_inline_tabs.html.twig in templates/bundles/SonataAdminBundle/CRUD/Association/edit_one_to_many_inline_tabs.html.twig with this code:

{% if sonata_admin.inline == 'custom' %}
  {% include "admin/fields/edit_one_to_many_inline_custom.html.twig" %}
  {# it's your customized template #}
{% else %}
  {% include "@!SonataAdmin/CRUD/Association/edit_one_to_many_inline_tabs.html.twig" %} 
{% endif %}

As you can see, you can configure as many as you want, even in the same form.

Upvotes: 0

lopsided
lopsided

Reputation: 2410

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

Upvotes: 18

Related Questions