nav - don't show empty categories
This commit is contained in:
parent
09e122af64
commit
bdb8613be4
9 changed files with 295 additions and 276 deletions
|
|
@ -1,27 +1,41 @@
|
|||
<?php
|
||||
|
||||
return function ($site) {
|
||||
$textsPage = $site->find('texts');
|
||||
$years = $textsPage->children();
|
||||
$texts = $years->children()->index();
|
||||
|
||||
function createCategories($textsPage) {
|
||||
$categories = array();
|
||||
|
||||
foreach ($textsPage->categories()->split() as $category) {
|
||||
$categories[$category] = array(
|
||||
'title' => $category,
|
||||
'texts' => array()
|
||||
);
|
||||
}
|
||||
return $categories;
|
||||
}
|
||||
|
||||
function fillCategoriesWithTexts($emptyCategories, $texts) {
|
||||
$filledCategories = $emptyCategories;
|
||||
foreach ($texts as $text) {
|
||||
try {
|
||||
$categories[$text->category()->value()]['texts'][] = $text;
|
||||
$textCategory = $text->category()->value();
|
||||
$filledCategories[$textCategory]['texts'][] = $text;
|
||||
} catch (\Throwable $th) {
|
||||
throw new Exception(json_encode($th->getFile() . ' : ' . $th->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
return $categories;
|
||||
// exclude empty categories
|
||||
return array_filter($filledCategories, function($category) {
|
||||
return count($category['texts']) > 0;
|
||||
});
|
||||
}
|
||||
|
||||
return function ($site) {
|
||||
$textsPage = $site->find('texts');
|
||||
$years = $textsPage->children();
|
||||
$texts = $years->children()->index();
|
||||
|
||||
$emptyCategories = createCategories($textsPage);
|
||||
|
||||
$filledCategories = fillCategoriesWithTexts($emptyCategories, $texts);
|
||||
|
||||
return $filledCategories;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue