From a0a46d24e75af1d30f00b9edee89127e0f8f5384 Mon Sep 17 00:00:00 2001 From: Monika Date: Thu, 11 Sep 2025 15:25:02 +0530 Subject: [PATCH] Added Tags filter for Joomla articles #2561 --- .../nucleus/particles/contentarray.html.twig | 5 ++ .../nucleus/particles/contentarray.yaml | 6 +++ .../Gantry/Joomla/Content/ContentFinder.php | 46 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/engines/joomla/nucleus/particles/contentarray.html.twig b/engines/joomla/nucleus/particles/contentarray.html.twig index 5fdb6ef02..a89ea7f77 100644 --- a/engines/joomla/nucleus/particles/contentarray.html.twig +++ b/engines/joomla/nucleus/particles/contentarray.html.twig @@ -17,6 +17,11 @@ {% set article_finder = joomla.finder('content', article_options).published(1).language() %} {% else %} {% set article_finder = joomla.finder('content').category(categories).published(1).language() %} + {# Tags - now supports both IDs and names #} + {% if filter.tags %} + {% set tags_options = {id: [filter.tags|replace(' ', '')]} %} + {% do article_finder.tags(tags_options) %} + {% endif %} {% endif %} {% set featured = filter.featured|default('include') %} diff --git a/engines/joomla/nucleus/particles/contentarray.yaml b/engines/joomla/nucleus/particles/contentarray.yaml index 1046a6aed..cc8ea9b68 100644 --- a/engines/joomla/nucleus/particles/contentarray.yaml +++ b/engines/joomla/nucleus/particles/contentarray.yaml @@ -31,6 +31,12 @@ form: description: Select the categories the articles should be taken from. overridable: false + article.filter.tags: + type: input.text + label: Tags + description: Enter tag IDs or names to filter the articles that should be shown. Use a comma-separated list (i.e. 1,2,tag1,tag2,5). You can mix IDs and names in the same list. + overridable: false + article.filter.articles: type: input.text label: Articles diff --git a/src/platforms/joomla/classes/Gantry/Joomla/Content/ContentFinder.php b/src/platforms/joomla/classes/Gantry/Joomla/Content/ContentFinder.php index e0cded7f2..dcd90ddb9 100644 --- a/src/platforms/joomla/classes/Gantry/Joomla/Content/ContentFinder.php +++ b/src/platforms/joomla/classes/Gantry/Joomla/Content/ContentFinder.php @@ -218,4 +218,50 @@ class ContentFinder extends Finder } } } + + public function tags($tagIds = []) + { + if (empty($tagIds['id'][0])) { + return $this; + } + + $input = $tagIds['id'][0]; + $tagIdsArray = []; + $tagNamesArray = []; + + // Separate IDs and names + $items = array_map('trim', explode(',', $input)); + + foreach ($items as $item) { + if (is_numeric($item)) { + $tagIdsArray[] = (int)$item; + } else { + $tagNamesArray[] = $item; + } + } + + // If we have tag names, get their IDs + if (!empty($tagNamesArray)) { + // Use the same database object as other methods in this class + $query = $this->db->getQuery(true) + ->select($this->db->quoteName('id')) + ->from($this->db->quoteName('#__tags')) + ->where($this->db->quoteName('title') . ' IN (' . implode(',', array_map([$this->db, 'quote'], $tagNamesArray)) . ')'); + + $this->db->setQuery($query); + $tagIdsFromNames = $this->db->loadColumn(); + + if (!empty($tagIdsFromNames)) { + $tagIdsArray = array_merge($tagIdsArray, $tagIdsFromNames); + } + } + + if (empty($tagIdsArray)) { + return $this; + } + + $this->query->join('INNER', '#__contentitem_tag_map AS t ON t.content_item_id = a.id'); + + return $this->where('t.tag_id', 'IN', $tagIdsArray)->where('t.type_alias', '=', 'com_content.article'); + } }