简体   繁体   English

Symfony Sonata Admin-在listView中添加字段类型url不起作用

[英]Symfony Sonata Admin - add field type url in listView not working

i'm trying to add a field type url in the list view of an entity, this is the link at the documentation -> https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/field_types.html#url . 我正在尝试在实体的列表视图中添加字段类型url,这是文档中的链接-> https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/field_types.html#url

This is my code, i've simply copied the documentation: 这是我的代码,我只复制了文档:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
            ->addIdentifier('name')
            ->add('url', 'url', [
                'url' => 'http://example.com'
            ]);
}

This seems to work but the column "Url" is always empty. 这似乎可行,但是“ Url”列始终为空。

在此处输入图片说明

I found the template of Sonata that is responsible to render this field -> @SonataAdmin/CRUD/list_url.html.twig . 我找到了负责渲染该字段的Sonata模板-> @ SonataAdmin / CRUD / list_url.html.twig。 Here is the code 这是代码

{% extends get_admin_template('base_list_field', admin.code) %}

{% block field %}
{% spaceless %}
   {% if value is empty %}
      
   {% else %}
      {% if field_description.options.url is defined %}
   ...

The problem is that value is always empty, i don't know what is this variable; 问题是总是空的,我不知道这个变量是什么。 and the documentation is not talking about any field named value. 并且文档没有讨论任何名为value的字段。

So you can achieve this by creating a template which simply contains a button with the URL you'd like to link to. 因此,您可以通过创建一个模板来实现此目的,该模板仅包含带有您要链接的URL的按钮。 See below: 见下文:

First we define a field on the list view which references a template, the type is null: 首先,我们在列表视图上定义一个引用模板的字段,类型为null:

->add('foo', null, [
    'template' => 'example/foobar.html.twig',
])

Inside our template we've just referenced, we can do the following: 在我们刚刚引用的模板中,我们可以执行以下操作:

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

{% block field %}
    <a class="btn btn-success" href="http://google.co.uk/">My Link</a>
{% endblock %}

and now you should see the button display as a column on the list view. 现在您应该在列表视图中看到按钮显示为一列。

It would be nice if the documented suggestion worked as intended, this solution is a work around. 如果有记录的建议按预期工作,那将是很好的选择,此解决方案可以解决。

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

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