public function doGenerate($location, array $parameters)
{
$urlAliasService = $this->repository->getURLAliasService();
$siteaccess = null;
if (isset($parameters['siteaccess'])) {
$siteaccess = $parameters['siteaccess'];
unset($parameters['siteaccess']);
}
if ($siteaccess) {
// We generate for a different SiteAccess, so potentially in a different language.
$languages = $this->configResolver->getParameter('languages', null, $siteaccess);
$urlAliases = $urlAliasService->listLocationAliases($location, false, null, null, $languages);
// Use the target SiteAccess root location
$rootLocationId = $this->configResolver->getParameter('content.tree_root.location_id', null, $siteaccess);
} else {
$languages = null;
$urlAliases = $urlAliasService->listLocationAliases($location, false);
$rootLocationId = $this->rootLocationId;
}
$queryString = '';
unset($parameters['language'], $parameters['contentId']);
if (!empty($parameters)) {
$queryString = '?' . http_build_query($parameters, '', '&');
}
if (!empty($urlAliases)) {
$path = $urlAliases[0]->path;
// Remove rootLocation's prefix if needed.
if ($rootLocationId !== null) {
$pathPrefix = $this->getPathPrefixByRootLocationId($rootLocationId, $languages, $siteaccess);
// "/" cannot be considered as a path prefix since it's root, so we ignore it.
if ($pathPrefix !== '/' && mb_stripos($path, $pathPrefix) === 0) {
$path = mb_substr($path, mb_strlen($pathPrefix));
} elseif ($pathPrefix !== '/' && !$this->isUriPrefixExcluded($path) && $this->logger !== null) {
// Location path is outside configured content tree and doesn't have an excluded prefix.
// This is most likely an error (from content edition or link generation logic).
$this->logger->warning("Generating a link to a location outside root content tree: '{$path}' is outside tree starting to location #{$rootLocationId}");
}
}
} else {
$path = $this->defaultRouter->generate(self::INTERNAL_CONTENT_VIEW_ROUTE, array('contentId' => $location->contentId, 'locationId' => $location->id));
}
$path = $path ?: '/';
// replace potentially unsafe characters with url-encoded counterpart
return strtr($path . $queryString, $this->unsafeCharMap);
}