Search code examples
symfonysonata-admin

Multiple fields (of type 'url') in single column of list in SonataAdmin


I have a symfony entity which (optionally) includes an 'attachment' file. I'd like to present two different links to the attachment (one to 'download', one to 'view') in a single column of the list entity list view. So far, I have this:

class MyEntityAdmin extends AbstractAdmin
{
  /*...*/
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
        /*...*/
            ->add('attachmentDownloadTitle', 'url', [
                'label' => 'download',
                'route' => [
                    'name' => 'get-my-entity-attachment',
                    'parameters' => ['disposition'  => 'download'],
                    'identifier_parameter_name' => 'id',
                    'absolute' => false,
                    ],
                ])
            ->add('attachmentViewTitle', 'url', [
                'label' => 'view',
                'route' => [
                    'name' => 'get-my-entity-attachment',
                    'parameters' => ['disposition'  => 'view'],
                    'identifier_parameter_name' => 'id',
                    'absolute' => false,
                    ],
                ])
        /*...*/
        ;
    }
}

which works as expected, except it (of course) creates two columns (one with the 'download' link, one with the 'view' link). Is there a way to 'join' multiple fields under one column in the list view? Or shall i go with the 'html' field type and construct the complete content of the field myself?


Solution

  • I think the easiest way is to use any type with a custom template that outputs the two links.

    ->add('filename', 'text', array(        
         'template' => '@AppBunle/Admin/CRUD/attachment_field.html.twig'
    ))
    

    attachment_field.html.twig

    {% extends '@SonataAdmin/CRUD/base_list_field.html.twig' %}
    
    {% block field %}
        <a href="#">{{ object.attachmentViewTitle }}</a>
        <a href="#">{{ object.attachmentDownloadTitle }}</a> 
    {% endblock %}