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

Feature Request: getChildPagesTagsRecursive() #35

Open
NicoHood opened this issue Aug 22, 2020 · 1 comment · May be fixed by #43
Open

Feature Request: getChildPagesTagsRecursive() #35

NicoHood opened this issue Aug 22, 2020 · 1 comment · May be fixed by #43

Comments

@NicoHood
Copy link

It would be nice to also get the tags of the pages below. This is useful when using '@self.descendants' as item collection.

@NicoHood
Copy link
Author

NicoHood commented Aug 22, 2020

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;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant