diff --git a/classes/taxonomylist.php b/classes/taxonomylist.php index ddb129f..f44e0b6 100644 --- a/classes/taxonomylist.php +++ b/classes/taxonomylist.php @@ -44,19 +44,33 @@ public function getChildPagesTags(PageInterface $current = null) if (!$child->isPage()) { continue; } - foreach($this->build($child->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; - } - } - } + $taxonomies = $this->mergeTaxonomies($taxonomies, $this->build($child->taxonomy())); + } + + return $taxonomies; + } + + /** + * Get taxonomy list with tags of all descendant pages. + * + * @return array + */ + public function getDescendantPagesTags(PageInterface $current = null) + { + /** @var PageInterface $current */ + if (null === $current) { + $current = Grav::instance()['page']; + } + + $pages = Grav::instance()['pages']; + $descendants = $pages->all($current)->remove($current->path())->pages(); + + $taxonomies = []; + foreach ($descendants->published() as $child) { + if (!$child->isPage()) { + continue; } + $taxonomies = $this->mergeTaxonomies($taxonomies, $this->build($child->taxonomy())); } return $taxonomies; @@ -97,4 +111,29 @@ protected function build(array $taxonomylist) return $list; } + + /** + * Merge two taxonomy arrays. + * + * @param array $taxonomies + * @param array $newTaxonomies + * @return array + */ + private function mergeTaxonomies(array $taxonomies, array $newTaxonomies) + { + foreach ($newTaxonomies 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; + } }