Open NicoHood opened 4 years ago
I am no php expert, so the following code should be reviewed first. That is my first time hacking php. Feel free to adapt it!
/**
* Get taxonomy list with only tags of the child pages.
* @param bool Include also recursively descendants of the child pages.
*
* @return array
*/
public function getChildPagesTags(bool $recursive = false)
{
$current = Grav::instance()['page'];
$taxonomies = $this->getChildPagesTagsInternal([], $current, $recursive);
return $taxonomies;
}
/**
* @internal
* @param array $taxonomies
* @param \Grav\Common\Page\Page $page
* @return array
*/
protected function getChildPagesTagsInternal(array $taxonomies, \Grav\Common\Page\Page $page, bool $recursive = false)
{
// Also parse all sub pages
if($recursive == true)
{
foreach ($page->children()->published() as $child) {
$taxonomies = $this->getChildPagesTagsInternal($taxonomies, $child, $recursive);
}
}
// Parse current page
foreach($this->build($page->taxonomy()) as $taxonomyName => $taxonomyValue) {
if (!isset($taxonomies[$taxonomyName])) {
$taxonomies[$taxonomyName] = $taxonomyValue;
} else {
foreach ($taxonomyValue as $value => $count) {
if (!isset($taxonomies[$taxonomyName][$value])) {
$taxonomies[$taxonomyName][$value] = $count;
} else {
$taxonomies[$taxonomyName][$value] += $count;
}
}
}
}
return $taxonomies;
}
It would be nice to also get the tags of the pages below. This is useful when using '@self.descendants' as item collection.