Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Sortable behavior in admin listing

Sortable behavior in admin listing

Edit this page

This is a full working example of how to implement a sortable feature in your Sonata admin listing

Background

A sortable behavior is already available for one-to-many relationships (https://docs.sonata-project.org/projects/SonataDoctrineORMAdminBundle/en/4.x/reference/form_field_definition/#advanced-usage-one-to-many). However there is no packaged solution to have some up and down arrows to sort your records such as showed in the following screen

Sortable listing

Pre-requisites

Configuration

  • you already have SonataAdmin and DoctrineORM up and running
  • you already have an Entity class for which you want to implement a sortable feature. For the purpose of the example we are going to call it Client.
  • you already have an Admin set up, in this example we will call it ClientAdmin

Bundles

  • install gedmo/doctrine-extensions bundle in your project (check stof/doctrine-extensions-bundle for easier integration in your project) and enable the sortable feature in your config
  • install runroom-packages/sortable-behavior-bundle at least version ^0.16 and enable it in config/bundles.php

The recipe

First of all we are going to add a position field in our Client entity:

1
2
3
#[Gedmo\SortablePosition]
#[ORM\Column(type: Types::INTEGER)]
private ?int $position = null;

Then we need to inject the Sortable listener. If you only have the Gedmo bundle enabled, you only have to add the listener to your services.yaml file and skip this step.

1
2
3
4
5
6
7
8
9
# config/services.yaml

services:
    gedmo.listener.sortable:
        class: Gedmo\Sortable\SortableListener
        calls:
            - [setAnnotationReader, ['@annotation_reader']]
        tags:
            - { name: doctrine.event_subscriber, connection: default }

If you have the stof/doctrine-extensions-bundle, you only need to enable the sortable feature in your configuration such as

1
2
3
4
5
6
# config/packages/stof_doctrine_extensions.yaml

stof_doctrine_extensions:
    orm:
        default:
            sortable: true

In our ClientAdmin we are going to add a custom action in the configureListFields method and use the default twig template provided in the RunroomSortableBehaviorBundle:

1
2
3
4
5
6
7
8
$list
    ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
        'actions' => [
            'move' => [
                'template' => '@RunroomSortableBehavior/sort.html.twig'
            ],
        ]
    ]);

In order to add new routes for these actions and to apply right sorting use Runroom\SortableBehaviorBundle\Admin\SortableAdminTrait :

1
2
3
4
5
6
7
8
9
10
11
// src/Admin/ClientAdmin.php

namespace App\Admin;

use Runroom\SortableBehaviorBundle\Admin\SortableAdminTrait;
use Sonata\AdminBundle\Admin\AbstractAdmin;

final class ClientAdmin extends AbstractAdmin
{
    use SortableAdminTrait;
}

Define Admin in services.yaml

1
2
3
4
5
6
7
# config/services.yaml

services:
    app.admin.client:
        class: App\Admin\ClientAdmin
        tags:
            - { name: sonata.admin, model_class: App\Entity\Client, manager_type: orm, label: 'Clients' }

Now we need to define sortable action:

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
// src/Admin/ClientAdmin.php

namespace App\Admin;

use Runroom\SortableBehaviorBundle\Admin\SortableAdminTrait;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;

final class ClientAdmin extends AbstractAdmin
{
    use SortableAdminTrait;

    protected function configureListFields(ListMapper $list): void
    {
        $list
            ->addIdentifier('name')
            ->add('enabled')
            ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
                'actions' => [
                    'move' => [
                        'template' => '@RunroomSortableBehavior/sort.html.twig'
                    ],
                ],
            ])
        ;
    }
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Be safe against critical risks to your projects and businesses

    Be safe against critical risks to your projects and businesses

    Version:

    Table of Contents

    • Background
    • Pre-requisites
      • Configuration
      • Bundles
    • The recipe

    Symfony footer

    Avatar of NickSdot, a Symfony contributor

    Thanks NickSdot for being a Symfony contributor

    2 commits • 90 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony