Sortable Trait Event listening?

I need to know when a model has been sorted using the Sortable trait, to update another model for synchronisation.
I dont fgind any event in the Sortable trait, is there any other way to accomplish that please?

i tried to liste for the afterSave event, but it does fire since the trait do a direct sql update in the database.

Hello @chris ,

have you tried with some js ?
the modules\backend\assets\js\october.treelist.js gives the opportunity to use event `move.oc.treelist’ which is triggered when a node on the tree is moved.

/*
 * TreeList Widget
 *
 * Supported options:
 *  - handle - class name to use as a handle
 *  - nested - set to false if sorting should be kept within each OL container, if using
 *             a handle it should be focused enough to exclude nested handles.
 * 
 * Events:
 * - move.oc.treelist - triggered when a node on the tree is moved.
 * 
 * Dependences:
 * - Sortable Plugin (october.sortable.js)
 */

Hi @Ladylain
thanks. I need to update a model in php after a model is reordered through the Sortable trait

maybe you could rewrite the onReorder function in your Controller that has ReorderController ?

I thought about this as well but the ReorderController is now deprecated in favour of the new

structure:
   showReorder: true

in the config_relation.yaml file

onReorder function also exist in \modules\backend\widgets\ListStructure.php

and this seems to fire a system event

1 Like

I found a way using php and extending the trait method in the model, because we dont want to copy/paste the trait method executing the reordeering.

in the model.php

use \October\Rain\Database\Traits\Sortable {
        setSortableOrder as public traitSetSortableOrder; 
    }
...

/**
 * extends sortable method
 */
public function setSortableOrder($itemIds, $referencePool = null)
{
    // execute the trait method
    $this->traitSetSortableOrder($itemIds, $referencePool = null);
    // then do our stuff here
}
2 Likes