/RunroomSortableBehaviorBundle

[READ-ONLY] Offers a sortable feature for your Symfony + Sonata Admin lists

Primary LanguagePHPMIT LicenseMIT

RunroomSortableBehaviorBundle

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

This bundle gives the ability to define sortable entities and to be able to sort the using Sonata Backoffice. It is inspired on: pixSortableBehaviorBundle.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require runroom-packages/sortable-behavior-bundle

Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    Runroom\SortableBehaviorBundle\RunroomSortableBehaviorBundle::class => ['all' => true],
];

Usage

This bundle checks if you are using Gedmo Sortable to handle the sort order of your entities, if not, it will use the default ORM implementation, where you will need to add entities on the configuration of the bundle manually. If you are using Gedmo, and don't want to change the default field name position, you don't need to configure anything for the bundle.

We provide a trait, so you can easily add the position field with the Gedmo configuration on each entity you want to be able to sort:

# src/Entity/Example.php

namespace App\Entity;

use Runroom\SortableBehaviorBundle\Behaviors\Sortable;

class Example
{
    use Sortable;
    // ... rest of your class
}

And then, on your admin class, you can add the SortableAdminTrait trait to be able to sort the entities on the list view:

# src/Admin/ExampleAdmin.php

namespace App\Admin;

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

class ExampleAdmin extends AbstractAdmin
{
    use SortableAdminTrait;

    protected function configureListFields(ListMapper $list): void
    {
        $list
            // ... rest of your list fields
            ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
                'actions' => [
                    // ... rest of your actions
                    'move' => [
                        'template' => '@RunroomSortableBehavior/sort.html.twig',
                    ],
                ],
            ]);
    }
}

And that's all, you should now see the sort buttons on the list view of your admin class.

Configuration

# config/packages/runroom_sortable_behavior.yaml

runroom_sortable_behavior:
    # position_handler can be any service id that implements the PositionHandlerInterface
    position_handler: Runroom\SortableBehaviorBundle\Service\GedmoPositionHandler # or Runroom\SortableBehaviorBundle\Service\ORMPositionHandler if gedmo is not found
    position_field:
        default: position # Default field name for the position
        # Only needed when not using Gedmo
        entities:
            App\Entity\Foobar: order
            App\Entity\Baz: rang
    # Only needed when not using Gedmo
    sortable_groups:
        entities:
            App\Entity\Baz: [group]

Use a draggable list instead of up/down buttons

In order to use a draggable list instead of up/down buttons, change the template in the move action to @RunroomSortableBehavior/sort_drag_drop.html.twig.

protected function configureListFields(ListMapper $list): void
{
    $list
        // ... rest of your list fields
        ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [
            'actions' => [
                // ... rest of your actions
                'move'   => [
                    'template' => '@RunroomSortableBehavior/sort_drag_drop.html.twig',
                    'enable_top_bottom_buttons' => true, // optional
                ],
            ],
        ]);
}

Contribute

The sources of this package are contained in the Runroom monorepo. We welcome contributions for this package on runroom/runroom-packages.

License

This bundle is under the MIT license.