actuel-inactuel/site/config/routes/virtual-category.php
2025-02-21 13:08:53 +01:00

33 lines
808 B
PHP

<?php
use Kirby\Uuid\Uuid;
return [
'pattern' => 'categories/(:any)',
'action' => function ($category) {
$allTexts = page('textes')->grandChildren();
$textsInCategory = $allTexts->filter(
fn($text) => Str::slug($text->category()) === $category
);
$children = new Pages();
foreach ($textsInCategory as $text) {
$children->add($text);
}
$title = $textsInCategory->first()->category();
return Page::factory([
'slug' => Str::slug($category) . '-' . Uuid::generate(),
'template' => 'category',
'model' => 'category',
'content' => [
'title' => $title,
'uuid' => Uuid::generate(),
],
'children' => $children->toArray(),
]);
},
];