-
Notifications
You must be signed in to change notification settings - Fork 14
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
Feature Request: getChildPagesTagsRecursive() #35
Comments
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;
} |
This was referenced Oct 8, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nice to also get the tags of the pages below. This is useful when using '@self.descendants' as item collection.
The text was updated successfully, but these errors were encountered: