Skip to content

Commit 78140d8

Browse files
[5.4] Add tag filter none (joomla#45274)
Add a new filter option when filtering for tags that allows you to filter all articles that have no tags at all. --------- Co-authored-by: Richard Fath <[email protected]>
1 parent 8772e36 commit 78140d8

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

administrator/components/com_content/forms/filter_articles.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@
9292
<field
9393
name="tag"
9494
type="tag"
95-
multiple="true"
9695
label="JTAG"
97-
hint="JOPTION_SELECT_TAG"
96+
multiple="true"
9897
mode="nested"
9998
custom="false"
99+
hint="JOPTION_SELECT_TAG"
100100
class="js-select-submit-on-change"
101-
/>
101+
default=""
102+
>
103+
<option value="0">JNONE_FILTER</option>
104+
</field>
102105

103106
<field
104107
name="level"

administrator/components/com_content/forms/filter_featured.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@
8484
custom="false"
8585
hint="JOPTION_SELECT_TAG"
8686
class="js-select-submit-on-change"
87-
/>
87+
default=""
88+
>
89+
<option value="0">JNONE_FILTER</option>
90+
</field>
8891

8992
<field
9093
name="level"

administrator/components/com_content/src/Model/ArticlesModel.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,13 @@ protected function getListQuery()
491491
}
492492

493493
if ($tag && \is_array($tag)) {
494-
$tag = ArrayHelper::toInteger($tag);
494+
$tag = ArrayHelper::toInteger($tag);
495+
$includeNone = false;
496+
497+
if (\in_array(0, $tag)) {
498+
$tag = array_filter($tag);
499+
$includeNone = true;
500+
}
495501

496502
$subQuery = $db->getQuery(true)
497503
->select('DISTINCT ' . $db->quoteName('content_item_id'))
@@ -504,23 +510,56 @@ protected function getListQuery()
504510
);
505511

506512
$query->join(
507-
'INNER',
513+
$includeNone ? 'LEFT' : 'INNER',
508514
'(' . $subQuery . ') AS ' . $db->quoteName('tagmap'),
509515
$db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
510516
);
511-
} elseif ($tag = (int) $tag) {
512-
$query->join(
513-
'INNER',
514-
$db->quoteName('#__contentitem_tag_map', 'tagmap'),
515-
$db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
516-
)
517+
518+
if ($includeNone) {
519+
$subQuery2 = $db->getQuery(true)
520+
->select('DISTINCT ' . $db->quoteName('content_item_id'))
521+
->from($db->quoteName('#__contentitem_tag_map'))
522+
->where($db->quoteName('type_alias') . ' = ' . $db->quote('com_content.article'));
523+
$query->join(
524+
'LEFT',
525+
'(' . $subQuery2 . ') AS ' . $db->quoteName('tagmap2'),
526+
$db->quoteName('tagmap2.content_item_id') . ' = ' . $db->quoteName('a.id')
527+
)
528+
->where(
529+
'(' . $db->quoteName('tagmap.content_item_id') . ' IS NOT NULL OR '
530+
. $db->quoteName('tagmap2.content_item_id') . ' IS NULL)'
531+
);
532+
}
533+
} elseif (is_numeric($tag)) {
534+
$tag = (int) $tag;
535+
536+
if ($tag === 0) {
537+
$subQuery = $db->getQuery(true)
538+
->select('DISTINCT ' . $db->quoteName('content_item_id'))
539+
->from($db->quoteName('#__contentitem_tag_map'))
540+
->where($db->quoteName('type_alias') . ' = ' . $db->quote('com_content.article'));
541+
542+
// Only show articles without tags
543+
$query->join(
544+
'LEFT',
545+
'(' . $subQuery . ') AS ' . $db->quoteName('tagmap'),
546+
$db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
547+
)
548+
->where($db->quoteName('tagmap.content_item_id') . ' IS NULL');
549+
} else {
550+
$query->join(
551+
'INNER',
552+
$db->quoteName('#__contentitem_tag_map', 'tagmap'),
553+
$db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
554+
)
517555
->where(
518556
[
519557
$db->quoteName('tagmap.tag_id') . ' = :tag',
520558
$db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_content.article'),
521559
]
522560
)
523561
->bind(':tag', $tag, ParameterType::INTEGER);
562+
}
524563
}
525564

526565
// Add the list ordering clause.

0 commit comments

Comments
 (0)