Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to get taxonomies of all descendant pages #43

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 51 additions & 12 deletions classes/taxonomylist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}