hic-et-nunc/site/controllers/index.php
isUnknown 23ab9dc1e7 Fix duplicate and empty categories in nav filter
Trim whitespace after splitting on comma and filter out empty strings
before deduplication, so categories like " Imprimés" and "Imprimés"
are correctly unified.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 15:36:41 +02:00

35 lines
No EOL
1.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
return function($page) {
function array_flatten($array) {
if (!is_array($array)) {
return false;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result = array_merge($result, array($key => $value));
}
}
return $result;
}
function getSingleCategories($pages) {
$categoriesRawFields = $pages->pluck('category'); // return ["A, B, C", "A, B"]
$splittedCategoriesFields = array_map(function($field) {
return explode(',', $field);
}, $categoriesRawFields); // return [["A", "B", "C",], ["A", "B"]]
$flattenedCategories = array_flatten($splittedCategoriesFields);
$trimmed = array_map('trim', $flattenedCategories);
$filtered = array_filter($trimmed, fn($v) => $v !== '');
$filters = array_unique($filtered);
return $filters;
}
return [
'categories' => getSingleCategories($page->children())
];
};