eZ\Publish\Core\FieldType\RichText\RichTextStorage::storeFieldData PHP Method

storeFieldData() public method

See also: eZ\Publish\SPI\FieldType\FieldStorage
public storeFieldData ( eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo, eZ\Publish\SPI\Persistence\Content\Field $field, array $context )
$versionInfo eZ\Publish\SPI\Persistence\Content\VersionInfo
$field eZ\Publish\SPI\Persistence\Content\Field
$context array
    public function storeFieldData(VersionInfo $versionInfo, Field $field, array $context)
    {
        /** @var \eZ\Publish\Core\FieldType\RichText\RichTextStorage\Gateway $gateway */
        $gateway = $this->getGateway($context);
        $document = new DOMDocument();
        $document->loadXML($field->value->data);
        $xpath = new DOMXPath($document);
        $xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
        // This will select only links with non-empty 'xlink:href' attribute value
        $xpathExpression = "//docbook:link[string( @xlink:href ) and not( starts-with( @xlink:href, 'ezurl://' )" . "or starts-with( @xlink:href, 'ezcontent://' )" . "or starts-with( @xlink:href, 'ezlocation://' )" . "or starts-with( @xlink:href, '#' ) )]";
        $links = $xpath->query($xpathExpression);
        if (empty($links)) {
            return false;
        }
        $urlSet = array();
        $remoteIdSet = array();
        $linksInfo = array();
        /** @var \DOMElement $link */
        foreach ($links as $index => $link) {
            preg_match('~^(ezremote://)?([^#]*)?(#.*|\\s*)?$~', $link->getAttribute('xlink:href'), $matches);
            $linksInfo[$index] = $matches;
            if (empty($matches[1])) {
                $urlSet[$matches[2]] = true;
            } else {
                $remoteIdSet[$matches[2]] = true;
            }
        }
        $urlIdMap = $gateway->getUrlIdMap(array_keys($urlSet));
        $contentIds = $gateway->getContentIds(array_keys($remoteIdSet));
        $urlLinkSet = array();
        foreach ($links as $index => $link) {
            list(, $scheme, $url, $fragment) = $linksInfo[$index];
            if (empty($scheme)) {
                // Insert the same URL only once
                if (!isset($urlIdMap[$url])) {
                    $urlIdMap[$url] = $gateway->insertUrl($url);
                }
                // Link the same URL only once
                if (!isset($urlLinkSet[$url])) {
                    $gateway->linkUrl($urlIdMap[$url], $field->id, $versionInfo->versionNo);
                    $urlLinkSet[$url] = true;
                }
                $href = "ezurl://{$urlIdMap[$url]}{$fragment}";
            } else {
                if (!isset($contentIds[$url])) {
                    throw new NotFoundException('Content', $url);
                }
                $href = "ezcontent://{$contentIds[$url]}{$fragment}";
            }
            $link->setAttribute('xlink:href', $href);
        }
        $field->value->data = $document->saveXML();
        return true;
    }