eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator::doGenerate PHP Method

doGenerate() public method

Entries in $parameters will be added in the query string.
public doGenerate ( eZ\Publish\API\Repository\Values\Content\Location $location, array $parameters ) : string
$location eZ\Publish\API\Repository\Values\Content\Location
$parameters array
return string
    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);
    }

Usage Example

 /**
  * @dataProvider providerTestDoGenerateRootLocation
  */
 public function testDoGenerateRootLocation(URLAlias $urlAlias, $isOutsideAndNotExcluded, $expected, $pathPrefix)
 {
     $excludedPrefixes = array('/products', '/shared');
     $rootLocationId = 456;
     $this->urlAliasGenerator->setRootLocationId($rootLocationId);
     $this->urlAliasGenerator->setExcludedUriPrefixes($excludedPrefixes);
     $location = new Location(array('id' => 123));
     $rootLocation = new Location(array('id' => $rootLocationId));
     $rootUrlAlias = new URLAlias(array('path' => $pathPrefix));
     $this->locationService->expects($this->once())->method('loadLocation')->with($rootLocationId)->will($this->returnValue($rootLocation));
     $this->urlAliasService->expects($this->once())->method('reverseLookup')->with($rootLocation)->will($this->returnValue($rootUrlAlias));
     $this->urlAliasService->expects($this->once())->method('listLocationAliases')->with($location, false)->will($this->returnValue(array($urlAlias)));
     if ($isOutsideAndNotExcluded) {
         $this->logger->expects($this->once())->method('warning');
     }
     $this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, array()));
 }