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>
35 lines
No EOL
1.1 KiB
PHP
35 lines
No EOL
1.1 KiB
PHP
<?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())
|
||
];
|
||
}; |