Hi,
I have the following Blueprint where I can select categories by Select. I would like to display these categories in exactly the same order in the frontend. Does anyone have an idea how I can implement this?
category:
label: Category
type: select
options:
- Lichterfeste
- Einkaufsnächte
- Firmenfeiern
- Sonstige Veranstaltungen
How are you fetching the categories in the frontend at the moment?
Like this.
<?php $eventItems = page('events')->children()->listed()->flip()->groupBy('category');
?>
<?php foreach($eventItems as $eventItem => $itemsPerCategory): ?>
You would have to sort the collection first, then group. Check out the custom sorting example here: Sorting | Kirby CMS
Thanks, but I don’t use structure fields. Does that still work?
That doesn’t matter, works the same for a pages collection.
Unfortunately, I don’t understand how I can implement this with a select. Do you have another tip for me? Thank you!
You only have to replace the values
$eventItems = page('events')->children()->listed();
// map an order field to each item of the collection
$eventItems = $eventItems->map(function($item) {
// array that maps every real value to a sorting number
$options = ['Lichterfeste'=>'0','Einkaufsnächte'=>'1','Firmenfeiern'=>'2','Sonstige Veranstaltungen'=>'3',];
//get the order number from the array based on the item's size value
$item->order = $options[$item->category()->value()];
return $item;
});
// finally, sort by order
$eventItems = $eventItems->sortBy('order', 'asc')->groupBy('category');
Not tested, but should work.