Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sherlockode/sonatasortablebundle
https://github.com/sherlockode/sonatasortablebundle
Last synced: about 4 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/sherlockode/sonatasortablebundle
- Owner: sherlockode
- Created: 2022-12-06T08:20:54.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-13T08:07:44.000Z (over 1 year ago)
- Last Synced: 2024-09-23T01:14:17.477Z (about 2 months ago)
- Language: PHP
- Size: 8.79 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Installation
============Make sure Composer is installed globally, as explained in the
[installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.Applications that use Symfony Flex
----------------------------------Open a command console, enter your project directory and execute:
```console
$ composer require sherlockode/sonata-sortable-bundle
```Applications that don't use Symfony Flex
----------------------------------------### Step 1: Download the Bundle
Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:```console
$ composer require sherlockode/sonata-sortable-bundle
```### Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the `config/bundles.php` file of your project:```php
// config/bundles.phpreturn [
// ...
Sherlockode\SonataSortableBundle\SherlockodeSonataSortableBundle::class => ['all' => true],
];
```Sortable behavior in admin listing
==================================Pre-requisites
--------------You need to have a working Sonata admin and to have installed and configured `gedmo/doctrine-extensions` (check `stof/doctrine-extensions-bundle` for easy integration).
The recipe
----------First of all, add a position property in your entity
```php
// src/Entity/Category.phpnamespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;/**
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
* @var int
*
* @Gedmo\SortablePosition
* @ORM\Column(name="position", type="integer")
*/
private $position;
}
```To change this position with Sonata, we need to add a new route in our admin:
```php
// src/Admin/CategoryAdmin.phpnamespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollectionInterface;class CategoryAdmin extends AbstractAdmin
{
// ...
/**
* @param RouteCollectionInterface $collection
*
* @return void
*/
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->add('move', $this->getRouterIdParameter().'/move/{direction}');
}
}
```Update the admin configuration to use our custom controller
```yaml
services:
admin.category:
class: 'App\Admin\CategoryAdmin'
arguments: [ ~, App\Entity\Category, 'Sherlockode\SonataSortableBundle\Controller\SortableController' ]
tags:
- { name: sonata.admin, manager_type: orm, label: Categories }
```Then, add default sort by position in the admin:
```php
// src/Admin/CategoryAdmin.phpnamespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridInterface;class CategoryAdmin extends AbstractAdmin
{
// ...
protected function configureDefaultSortValues(array &$sortValues): void
{
$sortValues[DatagridInterface::PAGE] = 1;
$sortValues[DatagridInterface::SORT_ORDER] = 'ASC';
$sortValues[DatagridInterface::SORT_BY] = 'position';
}
}
```Add controls to allow the user to change the position of items:
```php
// src/Admin/CategoryAdmin.phpnamespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;class CategoryAdmin extends AbstractAdmin
{
// ...
/**
* @inheritDoc
*/
protected function configureListFields(ListMapper $list): void
{
$list
// your other fields
->add(ListMapper::NAME_ACTIONS, null, [
'actions' => [
'move' => [
'template' => '@SherlockodeSonataSortable/list__action_move.html.twig',
],
],
])
;
}
}
```