generate() public method

It is possible to directly pass a Location object as the route name, as the ChainRouter allows it through ChainedRouterInterface. If $name is a route name, the "location" key in $parameters must be set to a valid eZ\Publish\API\Repository\Values\Content\Location object. "locationId" can also be provided. If the generator is not able to generate the url, it must throw the RouteNotFoundException as documented below.
See also: UrlAliasRouter::supports()
public generate ( string | eZ\Publish\API\Repository\Values\Content\Location $name, mixed $parameters = [], integer $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH ) : string
$name string | eZ\Publish\API\Repository\Values\Content\Location The name of the route or a Location instance
$parameters mixed An array of parameters
$referenceType integer The type of reference to be generated (one of the constants)
return string The generated URL
Beispiel #1
0
 /**
  * Converts internal links (ezcontent:// and ezlocation://) to URLs.
  *
  * @param \DOMDocument $document
  *
  * @return \DOMDocument
  */
 public function convert(DOMDocument $document)
 {
     $document = clone $document;
     $xpath = new DOMXPath($document);
     $xpath->registerNamespace("docbook", "http://docbook.org/ns/docbook");
     $linkAttributeExpression = "starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )";
     $xpathExpression = "//docbook:link[{$linkAttributeExpression}]|//docbook:ezlink";
     /** @var \DOMElement $link */
     foreach ($xpath->query($xpathExpression) as $link) {
         // Set resolved href to number character as a default if it can't be resolved
         $hrefResolved = "#";
         $href = $link->getAttribute("xlink:href");
         $location = null;
         preg_match("~^(.+://)?([^#]*)?(#.*|\\s*)?\$~", $href, $matches);
         list(, $scheme, $id, $fragment) = $matches;
         if ($scheme === "ezcontent://") {
             try {
                 $contentInfo = $this->contentService->loadContentInfo($id);
                 $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
                 $hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning("While generating links for richtext, could not locate " . "Content object with ID " . $id);
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice("While generating links for richtext, unauthorized to load " . "Content object with ID " . $id);
                 }
             }
         } else {
             if ($scheme === "ezlocation://") {
                 try {
                     $location = $this->locationService->loadLocation($id);
                     $hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
                 } catch (APINotFoundException $e) {
                     if ($this->logger) {
                         $this->logger->warning("While generating links for richtext, could not locate " . "Location with ID " . $id);
                     }
                 } catch (APIUnauthorizedException $e) {
                     if ($this->logger) {
                         $this->logger->notice("While generating links for richtext, unauthorized to load " . "Location with ID " . $id);
                     }
                 }
             } else {
                 $hrefResolved = $href;
             }
         }
         $hrefAttributeName = "xlink:href";
         // For embeds set the resolved href to the separate attribute
         // Original href needs to be preserved in order to generate link parameters
         // This will need to change with introduction of UrlService and removal of URL link
         // resolving in external storage
         if ($link->localName === "ezlink") {
             $hrefAttributeName = "href_resolved";
         }
         $link->setAttribute($hrefAttributeName, $hrefResolved);
     }
     return $document;
 }
 /**
  * @expectedException \LogicException
  */
 public function testGenerateWithContentIdWithMissingMainLocation()
 {
     $contentId = 456;
     $contentInfo = new ContentInfo(array('id' => $contentId, 'mainLocationId' => null));
     $parameters = array('some' => 'thing');
     $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH;
     $this->contentService->expects($this->once())->method('loadContentInfo')->with($contentId)->will($this->returnValue($contentInfo));
     $this->router->generate(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, $parameters + array('contentId' => $contentId), $referenceType);
 }
 /**
  * @covers eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter::__construct
  * @covers eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter::generate
  */
 public function testGenerateWithLocationAsParameter()
 {
     $locationId = 123;
     $location = new Location(array('id' => $locationId));
     $parameters = array('some' => 'thing');
     $absolute = false;
     $generatedLink = '/foo/bar';
     $this->urlALiasGenerator->expects($this->once())->method('generate')->with($location, $parameters, $absolute)->will($this->returnValue($generatedLink));
     $this->assertSame($generatedLink, $this->router->generate(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, $parameters + array('location' => $location), $absolute));
 }
 /**
  * Converts internal links (eznode:// and ezobject://) to URLs.
  *
  * @param \DOMDocument $xmlDoc
  *
  * @return string|null
  */
 public function convert(DOMDocument $xmlDoc)
 {
     foreach ($xmlDoc->getElementsByTagName("link") as $link) {
         $location = null;
         if ($link->hasAttribute('object_id')) {
             try {
                 $contentInfo = $this->contentService->loadContentInfo($link->getAttribute('object_id'));
                 $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning("While generating links for xmltext, could not locate " . "Content object with ID " . $link->getAttribute('object_id'));
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice("While generating links for xmltext, unauthorized to load " . "Content object with ID " . $link->getAttribute('object_id'));
                 }
             }
         }
         if ($link->hasAttribute('node_id')) {
             try {
                 $location = $this->locationService->loadLocation($link->getAttribute('node_id'));
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning("While generating links for xmltext, could not locate " . "Location with ID " . $link->getAttribute('node_id'));
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice("While generating links for xmltext, unauthorized to load " . "Location with ID " . $link->getAttribute('node_id'));
                 }
             }
         }
         if ($location !== null) {
             $link->setAttribute('url', $this->urlAliasRouter->generate($location));
         }
         if ($link->hasAttribute('anchor_name')) {
             $link->setAttribute('url', $link->getAttribute('url') . "#" . $link->getAttribute('anchor_name'));
         }
     }
 }
 /**
  * Converts internal links (eznode:// and ezobject://) to URLs.
  *
  * @param \DOMDocument $xmlDoc
  *
  * @return string|null
  */
 public function convert(DOMDocument $xmlDoc)
 {
     $xpath = new DOMXPath($xmlDoc);
     $elements = $xpath->query('//link|//embed|//embed-inline');
     /** @var \DOMElement $element */
     foreach ($elements as $element) {
         $location = null;
         if ($this->elementHasAttribute($element, 'object_id')) {
             try {
                 $contentInfo = $this->contentService->loadContentInfo($this->getElementAttribute($element, 'object_id'));
                 $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning('While generating links for xmltext, could not locate ' . 'Content object with ID ' . $this->getElementAttribute($element, 'object_id'));
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice('While generating links for xmltext, unauthorized to load ' . 'Content object with ID ' . $this->getElementAttribute($element, 'object_id'));
                 }
             }
         }
         if ($this->elementHasAttribute($element, 'node_id')) {
             try {
                 $location = $this->locationService->loadLocation($this->getElementAttribute($element, 'node_id'));
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning('While generating links for xmltext, could not locate ' . 'Location with ID ' . $this->getElementAttribute($element, 'node_id'));
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice('While generating links for xmltext, unauthorized to load ' . 'Location with ID ' . $this->getElementAttribute($element, 'node_id'));
                 }
             }
         }
         if ($location !== null) {
             $element->setAttribute('url', $this->urlAliasRouter->generate($location));
         }
         // Copy temporary URL if it exists and is not set at this point
         if (!$element->hasAttribute('url') && $element->hasAttribute(EmbedLinking::TEMP_PREFIX . 'url')) {
             $element->setAttribute('url', $element->getAttribute(EmbedLinking::TEMP_PREFIX . 'url'));
         }
         if ($this->elementHasAttribute($element, 'anchor_name')) {
             $element->setAttribute('url', $element->getAttribute('url') . '#' . $this->getElementAttribute($element, 'anchor_name'));
         }
     }
 }
Beispiel #6
0
 /**
  * Converts internal links (ezcontent:// and ezlocation://) to URLs.
  *
  * @param \DOMDocument $document
  *
  * @return \DOMDocument
  */
 public function convert(DOMDocument $document)
 {
     $document = clone $document;
     $xpath = new DOMXPath($document);
     $xpath->registerNamespace("docbook", "http://docbook.org/ns/docbook");
     $xpathExpression = "//docbook:link[starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )]";
     /** @var \DOMElement $link */
     foreach ($xpath->query($xpathExpression) as $link) {
         $location = null;
         preg_match("~^(.+)://([^#]*)?(#.*|\\s*)?\$~", $link->getAttribute("xlink:href"), $matches);
         list(, $scheme, $id, $fragment) = $matches;
         if ($scheme === "ezcontent") {
             try {
                 $contentInfo = $this->contentService->loadContentInfo($id);
                 $location = $this->locationService->loadLocation($contentInfo->mainLocationId);
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning("While generating links for richtext, could not locate " . "Content object with ID " . $id);
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice("While generating links for richtext, unauthorized to load " . "Content object with ID " . $id);
                 }
             }
         }
         if ($scheme === "ezlocation") {
             try {
                 $location = $this->locationService->loadLocation($id);
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->warning("While generating links for richtext, could not locate " . "Location with ID " . $id);
                 }
             } catch (APIUnauthorizedException $e) {
                 if ($this->logger) {
                     $this->logger->notice("While generating links for richtext, unauthorized to load " . "Location with ID " . $id);
                 }
             }
         }
         if ($location !== null) {
             $link->setAttribute('xlink:href', $this->urlAliasRouter->generate($location) . $fragment);
         }
     }
     return $document;
 }