4. List field definition

These fields are used to display the information inside the list table.

4.1. Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace Sonata\NewsBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;

final class PostAdmin extends AbstractAdmin
{
    protected function configureListFields(ListMapper $list): void
    {
        $list
            ->addIdentifier('title')
            ->add('author')
            ->add('enabled')
            ->add('tags')
            ->add('commentsEnabled')

            // add custom action links
            ->add('_action', 'actions', [
                'actions' => [
                    'show' => [],
                    'edit' => [],
                ]
            ])
        ;
    }
}

4.2. Types available

The most important option for each field is the type. The available types are the Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface::TYPE_* constant.

Note

For the FieldDescriptionInterface::TYPE_MANY_TO_ONE type, a link will be added to the related show action.

Note

Entities with a class as identifier value (ex. uuid) will resolve to the correct supported type:

use Ramsey\Uuid\Uuid;

class Example
{
    /**
     * @var \Ramsey\Uuid\Uuid
     *
     * @ORM\Column(type="uuid")
     * @ORM\Id
     */
    private $id;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
    }
}

If no type is set, the Admin class will use the type defined in the Doctrine mapping definition.

4.3. List actions

You can set actions for the list items by adding an ‘_action’ field in configureListFields:

$listMapper
    ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, []
        'actions' => [
            'show' => [],
            'edit' => [],
        ]
    ]);

Edit and Delete actions are enabled in the default configuration. You can add your own! Default template file is: @SonataAdmin/CRUD/list__action_[ACTION_NAME].html.twig

You can specify your own by setting up the ‘template’ option like so:

$listMapper
    ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
        'actions' => [
            'show' => [],
            'edit' => [],
            'delete' => ['template' => '@My/MyController/my_partial.html.twig'],
        ]
    ]);

4.4. Advance Usage

4.4.1. Displaying sub entity properties

If you need to display only one field from a sub entity or embedded object in a dedicated column, you can simply use the dot-separated notation:

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;

final class UserAdmin extends AbstractAdmin
{
    protected function configureListFields(ListMapper $list): void
    {
        $list
            ->addIdentifier('id')
            ->addIdentifier('firstName')
            ->addIdentifier('lastName')
            ->addIdentifier('address.street')
            ->addIdentifier('address.ZIPCode')
            ->addIdentifier('address.town')
        ;
    }
}

Note

This only makes sense when the prefix path is made of entities, not collections.

4.4.2. Custom template

If you need a specific layout for a row cell, you can define a custom template:

 namespace Sonata\MediaBundle\Admin;

 use Sonata\AdminBundle\Admin\AbstractAdmin;
 use Sonata\AdminBundle\Form\FormMapper;
 use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\ListMapper;
 use Sonata\AdminBundle\Show\ShowMapper;

final class MediaAdmin extends AbstractAdmin
 {
     protected function configureListFields(ListMapper $list): void
     {
         $list
             ->addIdentifier('id')
             ->add('image', 'string', ['template' => '@SonataMedia/MediaAdmin/list_image.html.twig'])
             ->add('custom', 'string', ['template' => '@SonataMedia/MediaAdmin/list_custom.html.twig'])
         ;
     }
 }

The related template:

1
2
3
4
5
6
7
8
{% extends '@SonataAdmin/CRUD/base_list_field.html.twig' %}

{% block field %}
    <div>
        <strong>{{ object.name }}</strong> <br/>
        {{ object.providername}} : {{ object.width }}x{{ object.height }} <br/>
    </div>
{% endblock %}

4.4.3. Custom route

Default route for a link is show (for FieldDescriptionInterface::TYPE_MANY_TO_ONE and FieldDescriptionInterface::TYPE_ONE_TO_ONE). Using this, the route can be customized as follows:

 namespace Sonata\MediaBundle\Admin;

 use Sonata\AdminBundle\Admin\AbstractAdmin;
 use Sonata\AdminBundle\Form\FormMapper;
 use Sonata\AdminBundle\Datagrid\DatagridMapper;
 use Sonata\AdminBundle\Datagrid\ListMapper;
 use Sonata\AdminBundle\Show\ShowMapper;

 final class MediaAdmin extends AbstractAdmin
 {
     protected function configureListFields(ListMapper $list): void
     {
         $list
             ->addIdentifier('field', null, [
                 'route' => [
                     'name' => 'edit'
                 ]
             ]);
     }
}