5

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.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
Anil Gupta
  • 2,329
  • 4
  • 24
  • 30

2 Answers2

14

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;
    }
}
Community
  • 1
  • 1
Dimitri
  • 141
  • 2
  • 3
7

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

webDEVILopers
  • 1,886
  • 1
  • 21
  • 35