Added Tags filter for Joomla articles #2561

This commit is contained in:
Monika
2025-09-11 15:25:02 +05:30
parent 9cee41221b
commit a0a46d24e7
3 changed files with 57 additions and 0 deletions
@@ -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') %}
@@ -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
@@ -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');
}
}