?
avatar brianteeman
brianteeman
2 Aug 2017

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

  • Category 2
    -- Category 3

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

avatar brianteeman brianteeman - open - 2 Aug 2017
avatar joomla-cms-bot joomla-cms-bot - change - 2 Aug 2017
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 2 Aug 2017
avatar tonypartridge
tonypartridge - comment - 2 Aug 2017

Hey @brianteeman so it's handled with a helper function:

public static function convertPathsToNames($tags)

within:

libraries/cms/helper/tags.php

avatar tonypartridge
tonypartridge - comment - 2 Aug 2017

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.

avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Aug 2017
Category com_tags
avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Aug 2017
Status New Discussion
avatar brianteeman
brianteeman - comment - 3 Aug 2017

@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

avatar phproberto
phproberto - comment - 3 Aug 2017

@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.

avatar phproberto
phproberto - comment - 3 Aug 2017

I have sent a fix for this:
#17396

avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Aug 2017
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2017-08-03 10:48:13
Closed_By franz-wohlkoenig
avatar joomla-cms-bot joomla-cms-bot - change - 3 Aug 2017
Closed_Date 2017-08-03 10:48:13 2017-08-03 10:48:14
Closed_By franz-wohlkoenig joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 3 Aug 2017
avatar joomla-cms-bot
joomla-cms-bot - comment - 3 Aug 2017
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 3 Aug 2017

closed as having PR #17396


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/17386.

avatar brianteeman
brianteeman - comment - 3 Aug 2017

thanks @phproberto I knew there was a bug there somewhere with the =nested

Add a Comment

Login with GitHub to post a comment