Back to TagsHelper class

Method convertPathsToNames

public static array
convertPathsToNames
(mixed $tags)
Function that converts tags paths into paths of names
Parameters
  • array $tags Array of tags
Returns
  • array
Since
  • 3.1
Class: TagsHelper
Project: Joomla

Method convertPathsToNames - Source code

/**
 * Function that converts tags paths into paths of names
 *
 * @param   array  $tags  Array of tags
 *
 * @return  array
 *
 * @since   3.1
 */
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_values(array_unique($aliases));
            $db = Factory::getDbo();
            $query = $db->getQuery(true)->select([$db->quoteName('alias'), $db->quoteName('title')])->from($db->quoteName('#__tags'))->whereIn($db->quoteName('alias'), $aliases, ParameterType::STRING);
            $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;
                                }
                            }
                            $tag->text = implode('/', $namesPath);
                        }
                    }
                }
            }
        }
    }
    return $tags;
}