Symfony 3.4: make Sonata list sortable using YAML for entitiy definition
I used the manual from https://github.com/sonata-project/SonataAdminBundle/blob/3.x/docs/cookbook/recipe_sortable_listing.rst, but it did not work for me. In fact, it contained too many bundles for my setup making the installation overly complicated. Furthermore, there is a pitfall when using YML files for entities. Using YML is supported, but not well documented and if annotations (as in the manual) are used together with YML, the annotations will be ignored. In order to get it working I needed to do the following steps.
Install the prerequisites gedmo/doctrine-extensions and pixassociates/sortable-behavior-bundle via composer. Activate the Sortable bundle in AppKernel:
new Pix\SortableBehaviorBundle\PixSortableBehaviorBundle(),
Add position field to Doctrine YML and update the entity PHP and database as usual:
fields: position: type: integer nullable: true column: position gedmo: - sortablePosition
In the Sonata Admin class add the following:
protected $datagridValues = [ '_page' => 1, '_sort_order' => 'ASC', '_sort_by' => 'position', ]; protected function configureListFields(ListMapper $listMapper) { ... $listMapper ->add('_action', null, [ 'actions' => [ 'move' => [ 'template' => '@PixSortableBehavior/Default/_sort.html.twig', 'enable_top_bottom_buttons' => true, ], ] ]); } protected function configureRoutes(RouteCollection $collection) { // ... $collection->add('move', $this->getRouterIdParameter().'/move/{position}'); }
Activate the Sortable Admin for this entity in services.yml:
admin.lineitem: class: Ond\ErpBundle\Admin\LineItemAdmin arguments: - ~ - Ond\ErpBundle\Entity\LineItem - 'PixSortableBehaviorBundle:SortableAdmin' tags: - { name: sonata.admin, manager_type: orm, label: Line Item, show_in_dashboard: false } public: true