2

What is the best way of querying the db to generate a list of tags matching a particular parent and display on the front end as a link with tag title and id?

This is for a custom joomla mvc component which uses two joomla tag fields: "categories" and "collection".

Once I have the list of tags I will then pass the tag id as a variable back to the page with jinput to limit the results in the components list view.

3

To generate a list of tags you can take getTagTreeArray() method from JHelperTags class as a base and modify it for you needs - you can populate the resulting array not only with the IDs, but also with the tag names. Then on frontend you can use getTagRoute() method of TagsHelperRoute class to get the tag link.

Hope this helps.

  • can you please vet the late post below? It seems like a thoughtful answer, but I don't know enough on this subject to differentiate good practice from bad practice. – mickmackusa Aug 7 '18 at 19:15
0
<?php
defined('_JEXEC') or die; // Joomla
$parent_tag_id=12345; // your tag id here
JLoader::register('TagsHelperRoute', JPATH_BASE . '/components/com_tags/helpers/route.php');
$tags_tree=JTable::getInstance('Tag', 'TagsTable')->getTree($parent_tag_id);
// Show children:
foreach($tags_tree as $tags_tree_id=>$tags_tree_item) {
    echo 'DEBUG: ';
    echo ' id:'    . $tags_tree_item->id;
    echo ' level:' . $tags_tree_item->level;
    echo ' path:'  . $tags_tree_item->path;
    $link=TagsHelperRoute::getTagRoute($tags_tree_item->id . ':' . $tags_tree_item->path);
    echo ' link: ' . JRoute::_( $link, true, 1);
    echo '<br>';
}

Additonally, to get all parent tag ordered form root to current depth level:

<?php
defined('_JEXEC') or die; // Joomla
$tag_id=12345; // your tag id here
$parent_tags=JTable::getInstance('Tag', 'TagsTable')->getPath($tag_id);
// Show parents and self:
foreach($parent_tags as $parent_tags_id=>$parent_tags_item) {
    echo 'DEBUG: ';
    echo ' id:'        . $parent_tags_item->id;
    echo ' level:'     . $parent_tags_item->level;
    echo ' path:'      . $parent_tags_item->path;
    echo ' published:' . $parent_tags_item-->published;
    echo '<br>';
}

Please note: you might need to check the published property, as you will get all tags, even the trashed ones. 1 is published, 0 is hidden, -2 is trashed, 2 is archived.

See https://api.joomla.org/cms-3/classes/Joomla.CMS.Router.Route.html also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.