I am trying to make nested tags in the searchtools tag filters display the same as nested categories. If you look at them now you will see that categories are
Category 1
But for tags they are
Tag1
Tag 1/Tag 2
Tag 1/Tag 2/Tag 3
For consistency they should be the same and for readability they should be using the -dash
Now for the bit i am stuck with. I have checked all the code I can see for generating a list of tags and as far as i can see the code is there to use the -dash and not to repeat the parent as text.
This is where I have looked
https://github.com/joomla/joomla-cms/blob/staging/libraries/cms/html/tag.php#L30
https://github.com/joomla/joomla-cms/blob/staging/libraries/src/Joomla/CMS/Form/Field/TagField.php#L220
Anyone see my error - reading those files or is there another file i have missed
Thanks in advance for any clues
Labels |
Added:
?
|
Try replacing the function with this one:
public static function convertPathsToNames($tags)
{
// We will replace path aliases with tag names
if ($tags)
{
// Create an array with all the aliases of the results
$aliases = array();
foreach ($tags as $tag)
{
if (!empty($tag->path))
{
if ($pathParts = explode('/', $tag->path))
{
$aliases = array_merge($aliases, $pathParts);
}
}
}
// Get the aliases titles in one single query and map the results
if ($aliases)
{
// Remove duplicates
$aliases = array_unique($aliases);
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('alias, title')
->from('#__tags')
->where('alias IN (' . implode(',', array_map(array($db, 'quote'), $aliases)) . ')');
$db->setQuery($query);
try
{
$aliasesMapper = $db->loadAssocList('alias');
}
catch (RuntimeException $e)
{
return false;
}
// Rebuild the items path
if ($aliasesMapper)
{
foreach ($tags as $tag)
{
$namesPath = array();
if (!empty($tag->path))
{
if ($pathParts = explode('/', $tag->path))
{
foreach ($pathParts as $alias)
{
if (isset($aliasesMapper[$alias]))
{
$namesPath[] = $aliasesMapper[$alias]['title'];
}
else
{
$namesPath[] = $alias;
}
}
$partsCount = count($namesPath);
$level = 1;
$text = '';
if ($partsCount > 1)
{
foreach ($namesPath as $key => $part)
{
if ($level !== $partsCount)
{
$text .= '--';
} else {
$text .= $part;
}
$level++;
}
$tag->text = $text;
} else {
$tag->text = implode('/', $namesPath);
}
}
}
}
}
}
}
return $tags;
}
It's just quickly chucked together. But should give you the desired result. I've tested very very limitedly.
Category | ⇒ | com_tags |
Status | New | ⇒ | Discussion |
@phproberto can you do me a favour please as you wrote at least some parts of this code. The proposed change from @tonypartridge above works BUT in that case why do we have the code I referr to in the first post. Surely some of this is duplicated and unused code? Or is my lack of developer skill missing something
@brianteeman you found a bug. Field is not detected as nested.
If you check the field definition you can see:
<field
name="tag"
type="tag"
label="JOPTION_FILTER_TAG"
description="JOPTION_FILTER_TAG_DESC"
mode="nested"
onchange="this.form.submit();"
>
<option value="">JOPTION_SELECT_TAG</option>
</field>
Note the mode="nested"
.
Then the field isNested() function does this comparison:
// If mode="nested" || ( mode not set & config = nested )
if (isset($this->element['mode']) && $this->element['mode'] === 'nested'
|| !isset($this->element['mode']) && $this->comParams->get('tag_field_ajax_mode', 1) == 0)
{
$this->isNested = true;
}
Which should be true because the field has mode="nested"
but this part:
$this->element['mode'] === 'nested'
is returning false because $this->element['mode']
is a SimpleXMLElement
object. It should be casted to string:
(string) $this->element['mode'] === 'nested'
I think that explains why you are confused.
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2017-08-03 10:48:13 |
Closed_By | ⇒ | franz-wohlkoenig |
Closed_Date | 2017-08-03 10:48:13 | ⇒ | 2017-08-03 10:48:14 |
Closed_By | franz-wohlkoenig | ⇒ | joomla-cms-bot |
Set to "closed" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/17386
closed as having PR #17396
thanks @phproberto I knew there was a bug there somewhere with the =nested
Hey @brianteeman so it's handled with a helper function:
within:
libraries/cms/helper/tags.php