Bisogna installarepixassociates/sortable-behavior-bundle e stof/doctrine-extensions-bundle
composer require stof/doctrine-extensions-bundle
composer require pixassociates/sortable-behavior-bundle
Aggiungere la configurazione in pix_sortable.yaml
Aggiungendo l’entitàée il campo in position_field (qui wconf, e il campo position)
pix_sortable_behavior:
db_driver: orm # mongodb default value : orm
position_field:
default: position #default value : position
entities:
App\Entity\Wconf: position
sortable_groups:
entities:
#AppBundle\Entity\Baz: [ group ]
stof_doctrine_extensions:
orm:
default:
sortable: true
Nel’entitàée bisogna créper un campo che va gémodificare la posizione (a caso la’posizione di chiamata)
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @Gedmo\SortablePosition
* @ORM\Column(type="integer")
*/
private $position;
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
Nel controller, bisogna aggiungere il ref in use, aggiungere la rotta e il pulsante
use Sonata\AdminBundle\Route\RouteCollection;
use Pix\SortableBehaviorBundle\Services\PositionORMHandler as PositionHandler;
public $last_position = 0;
private $positionService;
protected $datagridValues = array(
'_page' => 1,
'_sort_by' => 'position',
'_sort_order' => 'ASC',
);
public function setPositionService(PositionHandler $positionHandler)
{
$this->positionService = $positionHandler;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('_action', null, array(
'actions' => array(
'move' => array(
'template' => '@PixSortableBehavior/Default/_sort_drag_drop.html.twig',
'enable_top_bottom_buttons' => false,
),
),
))
;
}
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('move', $this->getRouterIdParameter().'/move/{position}');
}
Aggiungere poi gli asset javascript in config/sonata_admin.yaml, aggiungere il servizio gedmo e aggiungere la configurazione degli argomenti e delle chiamate
assets:
extra_javascripts:
- bundles/pixsortablebehavior/js/jquery-ui.min.js // if you haven't got jQuery UI yet.
- bundles/pixsortablebehavior/js/init.js
Nella configurazione dei servizi :
gedmo.listener.sortable:
class: Gedmo\Sortable\SortableListener
calls:
- [setAnnotationReader, ['@annotation_reader']]
tags:
- { name: doctrine.event_subscriber, connection: default }
admin.wconf:
class: App\Admin\WconfAdmin
arguments: [~, App\Entity\Wconf, 'PixSortableBehaviorBundle:SortableAdmin']
tags:
- { name: sonata.admin, manager_type: orm, label: "Configuration des types" }
public: true
calls:
- [setPositionService, ["@pix_sortable_behavior.position"]]
Adattare l’esempio alla versione installata
Gli esempi Symfony e Sonata rimangono legati alle versioni indicate nell’articolo. Symfony 4.x e Symfony 6.2 non sono più supportati: per un progetto attuale, utilizzate un ramo mantenuto e la documentazione corrispondente esattamente alle vostre dipendenze.
php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle
Prima di riprendere il codice, controllate le firme, i servizi, le rotte e i template interessati. Aggiungete poi un test funzionale che copra le autorizzazioni, il codice HTTP e il risultato visibile.
Riferimento : versioni di Symfony supportate.