Use custom column in Sonata Admin list

泄露秘密 提交于 2019-12-23 19:46:11

问题


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

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

But I still don't know why


回答1:


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' %}



回答2:


This ist most likely a path problem. The tricky thing is, that the configureList function won't give you any error (other like in configureForm). It displays the column, tries to match a property in your object but left it empty if there is no property. Double-check your path. I think you are pointing to the wrong file path.

You write

{# templates/template_test.html.twig #}

but you point to

'template' => 'template_test.html.twig',

So sonata is looking for app/Resources/view/template_test.html.twig

but your comment say its anywhere in app/Resources/view/templates/template_test.html.twig or somewhere else.



来源:https://stackoverflow.com/questions/50995176/use-custom-column-in-sonata-admin-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!