简体   繁体   中英

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 .

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.

在此处输入图片说明

I found the template of Sonata that is responsible to render this field -> @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.

So you can achieve this by creating a template which simply contains a button with the URL you'd like to link to. See below:

First we define a field on the list view which references a template, the type is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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