How to save a whole flex collection in order

Hey everbody,

since I can’t get my shiny new custom flex collection to sort itself by date, I thought I’d use a workaround and simply reorder the collection programmatically and then save it with the new order whenever an entry is added (so onFlexAfterSave).

The sorting seems to be simple enough:

public function onFlexAfterSave(Event $event)
    $type = $event['type'];
    $object = $event['object'];
    if($type === 'flex') {
        $collection = Grav::instance()->get('flex')->getCollection('termine');
        if ($collection) {
            $collection = $collection->sort(['datum' => 'ASC']);
            $objects = $collection->toArray();
            // ????

The sorting works nicely like this – I’ve dumped this onBuildPagesInitialized because apparently dumping doesn’t work in Admin?

But I have no idea how to go about saving the whole thing now…

Your help and ideas are much appreciated!

I figured out the ordering so currently I have no need to further go down this path.

(This is not a solution, I just no longer need one, so I’m leaving this as unsolved.)

1 Like