33 lines
1,013 B
PHP
33 lines
1,013 B
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);
|
|||
|
|
$filters = array_unique($flattenedCategories);
|
|||
|
|
|
|||
|
|
return $filters;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return [
|
|||
|
|
'categories' => getSingleCategories($page->children())
|
|||
|
|
];
|
|||
|
|
};
|