From bf8e049176e4cd506acb40ca5b69388fa86e71f0 Mon Sep 17 00:00:00 2001 From: Nicholas Krebs Date: Tue, 8 Oct 2024 14:24:01 +0200 Subject: [PATCH 1/2] add function to get all descendants of a page --- classes/taxonomylist.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/classes/taxonomylist.php b/classes/taxonomylist.php index ddb129f..ed5c352 100644 --- a/classes/taxonomylist.php +++ b/classes/taxonomylist.php @@ -62,6 +62,44 @@ public function getChildPagesTags(PageInterface $current = null) 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; + } + 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; + } + } + } + } + } + + return $taxonomies; + } + /** * @internal * @param array $taxonomylist From abc97838b8eb22a97f8823c6ab96745ffb8088e0 Mon Sep 17 00:00:00 2001 From: Nicholas Krebs Date: Tue, 8 Oct 2024 14:38:29 +0200 Subject: [PATCH 2/2] refactor duplicated lines to mergeTaxonomies() --- classes/taxonomylist.php | 57 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/classes/taxonomylist.php b/classes/taxonomylist.php index ed5c352..f44e0b6 100644 --- a/classes/taxonomylist.php +++ b/classes/taxonomylist.php @@ -44,19 +44,7 @@ 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; @@ -74,27 +62,15 @@ public function getDescendantPagesTags(PageInterface $current = null) $current = Grav::instance()['page']; } - $pages = Grav::instance()['pages']; - $descendants = $pages->all($current)->remove($current->path())->pages(); + $pages = Grav::instance()['pages']; + $descendants = $pages->all($current)->remove($current->path())->pages(); $taxonomies = []; foreach ($descendants->published() as $child) { 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; @@ -135,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; + } }