Skip to content

Commit

Permalink
Filter By Category
Browse files Browse the repository at this point in the history
Reference getgrav#9
  • Loading branch information
lcharette committed Apr 7, 2019
1 parent f860219 commit 6cad6bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions classes/taxonomylist.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ public function getChildPagesTags()
return $taxonomies;
}

/**
* Get taxonomy list with only tags of the specified category.
*
* @param string $category
* @return array
*/
public function getCategoryPagesTags($category = '')
{
$taxonomylist = Grav::instance()['taxonomy']->taxonomy();
$removed = [];

foreach ($taxonomylist['category'] as $cat => $entries) {
if ($cat != $category) {
$removed = array_merge($removed, array_keys($entries));
unset($taxonomylist['category'][$cat]);
}
}

foreach ($taxonomylist['tag'] as $tag => $entries) {
foreach ($entries as $url => $data) {
if (in_array($url, $removed)) {
unset($taxonomylist['tag'][$tag][$url]);
}
}
}

// Remove empty tags
foreach ($taxonomylist['tag'] as $tag => $entries) {
if (empty($entries)) {
unset($taxonomylist['tag'][$tag]);
}
}

return $this->build($taxonomylist);
}

/**
* @internal
* @param array $taxonomylist
Expand Down
2 changes: 1 addition & 1 deletion templates/partials/taxonomylist.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set taxlist = children_only is defined ? taxonomylist.getChildPagesTags() : taxonomylist.get() %}
{% set taxlist = children_only is defined ? taxonomylist.getChildPagesTags() : category is defined ? taxonomylist.getCategoryPagesTags(category) : taxonomylist.get() %}

{% if taxlist %}
<span class="tags">
Expand Down

0 comments on commit 6cad6bb

Please sign in to comment.