eZ\Publish\Core\FieldType\RichText\Converter\Link::convert PHP Method

convert() public method

Converts internal links (ezcontent:// and ezlocation://) to URLs.
public convert ( DOMDocument $document ) : DOMDocument
$document DOMDocument
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);
                    }
                }
            } elseif ($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;
    }

Usage Example

Example #1
0
 /**
  * Test logging of bad content links.
  *
  * @dataProvider providerBadContentLink
  */
 public function testConvertBadContentLink($input, $output, $contentId, $exception, $logType, $logMessage)
 {
     $inputDocument = new DOMDocument();
     $inputDocument->loadXML($input);
     $contentService = $this->getMockContentService();
     $locationService = $this->getMockLocationService();
     $urlAliasRouter = $this->getMockUrlAliasRouter();
     $logger = $this->getMock('Psr\\Log\\LoggerInterface');
     $logger->expects($this->once())->method($logType)->with($this->equalTo($logMessage));
     $contentService->expects($this->once())->method('loadContentInfo')->with($this->equalTo($contentId))->will($this->throwException($exception));
     $converter = new Link($locationService, $contentService, $urlAliasRouter, $logger);
     $outputDocument = $converter->convert($inputDocument);
     $expectedOutputDocument = new DOMDocument();
     $expectedOutputDocument->loadXML($output);
     $this->assertEquals($expectedOutputDocument, $outputDocument);
 }
All Usage Examples Of eZ\Publish\Core\FieldType\RichText\Converter\Link::convert