Show child category tags on category list view

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Show child category tags on category list view

Post by andrewrv » Tue May 30, 2023 12:55 am

Joomla 4.3.1 on PHP 8.0
I created an override /html/com_content/category/default_children.php
I can get the category id and display it for reference in the list by adding <?php echo $child->id; ?> where the child category titles are displayed

Code: Select all

<h3 class="page-header item-title"><a href="<?php echo Route::_(RouteHelper::getCategoryRoute($child->id, $child->language)); ?>">
                    <?php echo $this->escape($child->title); ?></a>
                    <?php if ($this->params->get('show_cat_num_articles', 1)) : ?>
                        <span class="badge bg-info tip hasTooltip" title="<?php echo HTMLHelper::_('tooltipText', 'COM_CONTENT_NUM_ITEMS'); ?>">
                            <?php echo $child->getNumItems(true); ?>
                        </span>
                    <?php endif; ?>
                    <?php if (count($child->getChildren()) > 0 && $this->maxLevel > 1) : ?>
                        <a href="#category-<?php echo $child->id; ?>" data-bs-toggle="collapse" class="btn btn-sm float-end" aria-label="<?php echo Text::_('JGLOBAL_EXPAND_CATEGORIES'); ?>"><span class="icon-plus" aria-hidden="true"></span></a>
                    <?php endif; ?>
                  <?php echo $child->id; ?>
                </h3>
But I am struggling to display the tags, I tried multiple variations of the examples here;
https://docs.joomla.org/J3.x:Using_Tags_in_an_Extension
https://docs.joomla.org/Tags_API_Guide
I have these defined

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\Component\Content\Site\Helper\RouteHelper;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Helper\TagsHelper;
and the following

Code: Select all

<?php
$th = new TagsHelper();
$result = $th->getItem->Tags->$child->id;
echo $result;
?>
and $result is empty

Does anyone know how to display the category tags? or what I am missing here?

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2906
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Show child category tags on category list view

Post by SharkyKZ » Tue May 30, 2023 5:57 am

Code: Select all

$th = new TagsHelper();
$tags = $th->getItemTags('com_content.category', $child->id);

andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Re: Show child category tags on category list view

Post by andrewrv » Tue May 30, 2023 11:32 am

Thanks, I changed that and $tags is empty

Code: Select all

$th = new TagsHelper();
$tags = $th->getItemTags('com_content.category', $child->id);
echo $tags;
$tags is empty

I added two more lines using https://docs.joomla.org/Tags_API_Guide as it looks like the tags are in an array, it's quite confusing as it's not clear to me what refers to Joomla 3, Joomla 4 or both.

Code: Select all

$th = new TagsHelper();
$tags = $th->getItemTags('com_content.category', $child->id);
echo $tags;             
$currentTagnames = $tagsHelper->getTagNames(explode(',', $tags));
echo "Tags: " . implode(',', $currentTagnames);
but this gives the error View not found [name, type, prefix]: search, error, ModuleView
if I use getTagNames

Code: Select all

$th = new TagsHelper();
$tags = $th->getTagNames('com_content.category', $child->id);             
echo $tags; 
$tags is "Array"

andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Re: Show child category tags on category list view

Post by andrewrv » Tue May 30, 2023 3:45 pm

I am still unable to display the tags, latest attempts below.

If i pass the category id to make sure it has a valid value

Code: Select all

<?php
$th = new TagsHelper();
$tags = $th->getItemTags('com_content.category', $child->id);   
$result = $th->getItemTags('com_content.category', 887); 
echo $tags;            
echo $result;                 
?>
both $tags and $result are "Array"
on https://docs.joomla.org/Tags_API_Guide it shows the id first and also the double quotes are used instead of single

Code: Select all

$result = $th->getTagIds(23, "com_content.article");

i tried

Code: Select all

$result = $th->getItemTags(887, 'com_content.category');
and

Code: Select all

$result = $th->getItemTags(887, "com_content.category"); 
and $result is "Array" in both cases

MarkRS
Joomla! Explorer
Joomla! Explorer
Posts: 329
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: Show child category tags on category list view

Post by MarkRS » Tue May 30, 2023 4:28 pm

Yes, "echo" doesn't print array contents. You want print_r or var_dump.

Single or double quotes probably don't make any difference in your context. In double quotes php will expand any variables you put in the string, singles won't.
It's a community, the more we all contribute, the better it will be.

andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Re: Show child category tags on category list view

Post by andrewrv » Tue May 30, 2023 4:49 pm

Thanks, this is a good information and I'm working on it, I have several thousand categories with tags and assumed that if I could tag categories and sub categories the tags would just display on the category list pages.

andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Re: Show child category tags on category list view

Post by andrewrv » Tue May 30, 2023 4:49 pm

Thanks, this is a good information and I'm working on it, I have several thousand categories with tags and assumed that if I could tag categories and sub categories the tags would just display on the category list pages.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2906
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Show child category tags on category list view

Post by SharkyKZ » Tue May 30, 2023 5:06 pm

getItemTags() returns an array of objects containing tag information. To display just tag names you can use something like this:

Code: Select all

echo implode(', ', array_column($result, 'title'));
Or if you want something more complex, you need to iterate over the array.

andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Re: Show child category tags on category list view

Post by andrewrv » Tue May 30, 2023 11:26 pm

Thanks, that didn't work but I think you have pointed me in the right direction, I will look at the database.

andrewrv
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Apr 15, 2009 10:26 pm

Re: Show child category tags on category list view

Post by andrewrv » Fri Jun 02, 2023 1:49 am

Thanks for the help, finally I can display the titles of the tags

Code: Select all

$th = new TagsHelper();
$tags = $th->getItemTags('com_content.category', $child->id); 
echo implode(', ', array_column($tags, 'title'));
So this displays the basic tag title in the category list as tagtitlea, tagtitleb and so on, next I will clean this up, add styles, and links to each tag


Post Reply

Return to “Joomla! 4.x Coding”