Skip to content

Commit

Permalink
EZP-29370: ezxmltext -> ricktext conversion: links inside links (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarl authored and andrerom committed Jul 3, 2018
1 parent cb56619 commit 6198e90
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/FieldType/XmlText/Converter/RichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,15 @@ protected function checkEmptyEmbedTags(DOMDocument $inputDocument, $contentField
}

/**
* We need to lookup object_id/node_id for links which refers to object_remote_id or node_object_id.
*
* Before calling this function, make sure you are logged in as admin, or at least have access to all the objects
* being linked to in the $document.
* @param DOMDocument $document
* @param $contentFieldId
*/
protected function checkLinkTags(DOMDocument $document, $contentFieldId)
protected function fixLinksWithRemoteIds(DOMDocument $document, $contentFieldId)
{
$xpath = new DOMXPath($document);

Expand Down Expand Up @@ -394,6 +396,34 @@ protected function checkLinkTags(DOMDocument $document, $contentFieldId)
}
}

/**
* ezxmltext may contain link elements below another link element. This method flattens such structure.
*
* @param DOMDocument $document
* @param $contentFieldId
*/
protected function flattenLinksInLinks(DOMDocument $document, $contentFieldId)
{
$xpath = new DOMXPath($document);

// Get all link elements which are child of a link
$xpathExpression = '//link[parent::link]';

$links = $xpath->query($xpathExpression);

foreach ($links as $link) {
// Move link to parent
$targetElement = $link->parentNode->parentNode;
$parentLink = $link->parentNode;
$targetElement->insertBefore($link, $parentLink);

// We want parent link to be listed first.
$targetElement->insertBefore($parentLink, $link);

$this->logger->warning("Found nested links. Flatten links where contentobject_attribute.id=$contentFieldId");
}
}

/**
* Before calling this function, make sure you are logged in as admin, or at least have access to all the objects
* being embedded and linked to in the $inputDocument.
Expand All @@ -409,7 +439,8 @@ public function convert(DOMDocument $inputDocument, $checkDuplicateIds = false,
{
$this->removeComments($inputDocument);
$this->checkEmptyEmbedTags($inputDocument, $contentFieldId);
$this->checkLinkTags($inputDocument, $contentFieldId);
$this->fixLinksWithRemoteIds($inputDocument, $contentFieldId);
$this->flattenLinksInLinks($inputDocument, $contentFieldId);

try {
$convertedDocument = $this->getConverter()->convert($inputDocument);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<section
xmlns:image="http://ez.no/namespaces/ezpublish3/image/"
xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"
xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/">
<paragraph>paragraph text and (
<link url_id="8">
<link url_id="42">some link text</link>
</link>), after link.
</paragraph>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
warning:Found nested links. Flatten links where contentobject_attribute.id=
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<section
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>paragraph text and (
<link xlink:href="ezurl://8" xlink:show="none"/><link xlink:href="ezurl://42" xlink:show="none">some link text</link>), after link.
</para>
</section>

0 comments on commit 6198e90

Please sign in to comment.