Search code examples
symfonysonata-adminsonata

Use custom column in Sonata Admin list


I created a project with Symfony 4.1, and install Sonata Admin Bundle.

In a listing of my categories, I try to add a column which is not related to a field of Category

So I did

/* Admin/CategoryAdmin.php */

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('test_column', 'string', [
            'template' => 'template_test.html.twig',
        ]);
}

And my template.

{# templates/template_test.html.twig #}

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field %}
    TEST
{% endblock %}

The column is created, but it's empty. What did I do wrong? Here's my test project: https://github.com/AntoineLemaire/sonata-admin-issue/commits/master

I had a other big project with Symfony 3.4 where it's working with no problem, so I created a fresh projet in 3.4, but I got the same issue.

No error message, juste blank for my column

---------- EDIT -----------

I had a better look, and it seams that the compiled template does not match my template

enter image description here

On my old big Symfony3.4 projet, compiled template is the same as template.

But I still don't know why


Solution

  • Ad yceruto said in the comments, the notation of my twig extends was not good:

    {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
    

    instead of

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