Question

I would like to override a CRUD list template on a per Admin / entity basis.

Assume I have an Admin class called MyAdminClass. I want to override the CRUD list template for that class, but not the whole application by adding templates to app/Resources/SonataAdminBundle/Views/CRUD. How can I achieve this?

Also, how would I override a group of templates rather than just one?

Was it helpful?

Solution

Form the documentation:

If you wish, you can specify custom templates on a per Admin mapping basis. Internally, the CRUDController fetches this information from the Admin class instance, so you can specify the templates to use in the Admin service definition:

XML:

<service id="sonata.admin.post" class="Acme\DemoBundle\Admin\PostAdmin">
    <tag name="sonata.admin" manager_type="orm" group="Content" label="Post"/>
    <argument />
    <argument>Acme\DemoBundle\Entity\Post</argument>
    <argument />
    <call method="setTemplate">
        <argument>edit</argument>
        <argument>AcmeDemoBundle:PostAdmin:edit.html.twig</argument>
    </call>
</service>

Yaml:

services:
    sonata.admin.post:
        class: Acme\DemoBundle\Admin\PostAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
        arguments:
            - ~
            - Acme\DemoBundle\Entity\Post
            - ~
        calls:
            - [ setTemplate, [edit, AcmeDemoBundle:PostAdmin:edit.html.twig]]

An example of using setTemplates:

XML:

<service id="sonata.admin.post" class="Acme\DemoBundle\Admin\PostAdmin">
    <!-- ... -->

    <call method="setTemplates">
        <argument type="collection">
            <argument key="edit">
                AcmeDemoBundle:PostAdmin:edit.html.twig
            </argument>
            <argument key="list">
                AcmeDemoBundle:PostAdmin:list.html.twig
            </argument>
        </argument>
    </call>
</service>

Yaml:

services:
    sonata.admin.post:
        # ...

        calls:
            - [setTemplates, [{edit: AcmeDemoBundle:PostAdmin:edit.html.twig, list: AcmeDemoBundle:PostAdmin:list.html.twig }]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top