3. Custom list template

We can also modify the template for the entire list – for example, to add a footer or extra buttons.

src/Admin/BookAdmin.php

    protected function configure(): void
    {
        $this->setTemplate('list', 'admin/book/list/list.html.twig');
    }

templates/admin/book/list/list.html.twig

{% extends '@SonataAdmin/CRUD/base_list.html.twig' %}

{% block list_footer %}
    {{ parent() }}


    <div class="box-footer">
        <div class="form-inline clearfix">
            Test
            <a href="{{ admin.generateUrl('import') }}" class="btn btn-primary">Import</a>
        </div>
    </div>
{% endblock %}

{% block list_table %}
    {{ parent() }}


    <div class="col-xs-12 col-md-12 mb-3" style="margin-top: -80px;">
        <div class="box box-primary">
             <div class="box-header with-border">
                <h3 class="box-title">TEST</h3>
            </div>
             <div class="box-body">
                Test               
                <a href="{{ admin.generateUrl('import') }}" class="btn btn-primary">Import</a>
            </div>
        </div>
    </div>
{% endblock %}

4. Custom template for show pages

On the admin side, we handle the show page in the same way as the list view.

src/Admin/BookAdmin.php

    protected function configureShowFields(ShowMapper $show): void
    {
        $show
            ->add('coverMedia', null, [
                'label' => 'book.field.cover_media',
                'template' => 'admin/common/show/media_image.html.twig',
            ])

For the preview view, we need to extend a different template: base_show_field
vendor/sonata-project/admin-bundle/src/Resources/views/CRUD/base_show_field.html.twig

The section we want to modify is the field_value block.

templates/admin/common/show/media_image.html.twig

{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}
 
{% block field_value %}
    {% if value is not empty %}
        {{ sonata_thumbnail(
            value,
            field_description.option('preview_format', 'small'),
            field_description.option('preview_options', [])
        ) }}
    {% endif %}
{% endblock %}

Sonata Admin: How to Add a Custom Template (part 1)

Sonata Admin: How to Add a Custom Template (part 3)