Search code examples
templatesdoctrinetwigsonata

Understanding the template mechanism of sonata / twig


I am struggling with the template engine of symfony/sonata ... normally i am familiar with inheritance and object-oriented design, but i don't understand this.

My target is, to override the DoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig because i believe, the list for the show function in my admin class is rendered there. I need it, because i want to show my data not only in a plain list like sonata is doing it now. I only want to override this for the show function of one explicit admin in my bundle and not in general!

According to the sonata documentation, i am only able to override the general template "SonataAdminBundle:CRUD:base_show.html.twig" by injecting a own inherited version of them to my service

- [ setTemplate, [show, AcmeBundle:TestAdmin:show.html.twig]]

The problem is, the base template includes / involves many other templates, sometimes accessed by object functions injected in the template by php.

How can i access/override the

DoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig

so that mine version is used instead of vendors one? Have i to override all chaining templates?

DoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig extends SonataAdminBundle:CRUD:base_show_field.html.twig

But i dont know, how SonataAdminBundle:CRUD:base_show_field.html.twig comes to the SonataAdminBundle:CRUD:base_show.html.twig. I cant find suiting code.

Thanks


Solution

  • You can override templates on a field by field basis instead of overriding the whole show template.

    $showMapper->add(
        'my_field',
        null, // let the type guesser do its thing
        ['template' => 'whatever_you_need.html.twig']
    );
    

    It this works and you can't find it in the docs, please consider contributing a paragraph about it.