5
votes

how can we override sonata bundle layout for a single Admin class like i have created 3 Admin Class userAdmin, productAdmin, ticketAdmin now i want to override ticketAdmin edit action and edit template and add some extra code there.

2

2 Answers

14
votes

You can use :

Controller :

custom action in SonataAdminBundle

Template :

// in your admin class
public function getTemplate($name)
{
    switch ($name) {
        case 'edit':
            return 'AcmeMyBundle::my-custom-edit.html.twig';
            break;
        default:
            return parent::getTemplate($name);
            break;
    }
}
7
votes

If you don't want to create an extra controller you can use this method mentioned in the docs:

Admin's documentation - Reference - Templates (master) - 20.6. Configuring templates

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]]

And put your template in Resources/views/PostAdmin/edit.html.twig. Just copy the original template from the SonataAdmin Bundle and start overriding.

Blogged at: Override list view twig template in SonataAdminBundle – webDEVILopers Blog