简体   繁体   English

在Sonata管理员列表中使用自定义列

[英]Use custom column in Sonata Admin list

I created a project with Symfony 4.1, and install Sonata Admin Bundle. 我用Symfony 4.1创建了一个项目,并安装了Sonata Admin Bundle。

In a listing of my categories, I try to add a column which is not related to a field of Category 在我的类别列表中,我尝试添加一个与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 这是我的测试项目: 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. 我有一个与Symfony 3.4的其他大项目,它没有问题,所以我在3.4中创建了一个新的项目,但我遇到了同样的问题。

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. 在我的旧的大型Symfony3.4项目中,编译的模板与模板相同。

But I still don't know why 但我还是不知道为什么

Ad yceruto said in the comments, the notation of my twig extends was not good: Ad yceruto在评论中说,我的枝条延伸的符号并不好:

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

instead of 代替

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

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). 棘手的是,configureList函数不会给你任何错误(其他类似于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 所以sonata正在寻找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. 但你的评论在app/Resources/view/templates/template_test.html.twig或其他任何地方都有说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM