Skip to content
Snippets Groups Projects
Commit 5a2ee566 authored by Marco Villegas's avatar Marco Villegas Committed by Lucas Hedding
Browse files

Issue #3427939 by marvil07, heddn: Replace text content mode for dom_str_replace

parent a70a881d
Branches
Tags
1 merge request!90Add text mode to dom_str_replace migrate process plugin
Pipeline #541356 canceled
......@@ -19,6 +19,7 @@ use Drupal\migrate\Row;
* - mode: What to modify. Possible values:
* - attribute: One element attribute.
* - element: An element name.
* - text: The element text content.
* - xpath: XPath query expression that will produce the \DOMNodeList to walk.
* - attribute_options: A map of options related to the attribute mode. Required
* when mode is attribute. The keys can be:
......@@ -70,6 +71,12 @@ use Drupal\migrate\Row;
* search: '/foo-(\d+)/'
* replace: 'bar-$1'
* -
* plugin: dom_str_replace
* mode: text
* xpath: '//a'
* search: 'Find more information here'
* replace: 'More information'
* -
* plugin: dom
* method: export
* @endcode
......@@ -96,6 +103,7 @@ class DomStrReplace extends DomProcessBase {
'attribute_options' => NULL,
],
'element' => [],
'text' => [],
],
'search' => NULL,
'replace' => NULL,
......@@ -218,6 +226,9 @@ class DomStrReplace extends DomProcessBase {
case 'element':
return $node->nodeName;
case 'text':
return $node->textContent;
}
}
......@@ -231,6 +242,7 @@ class DomStrReplace extends DomProcessBase {
switch ($this->configuration['mode']) {
case 'attribute':
case 'element':
case 'text':
return $this->configuration['search'];
}
}
......@@ -245,6 +257,7 @@ class DomStrReplace extends DomProcessBase {
switch ($this->configuration['mode']) {
case 'attribute':
case 'element':
case 'text':
return $this->configuration['replace'];
}
}
......@@ -299,6 +312,10 @@ class DomStrReplace extends DomProcessBase {
}
$html_node->parentNode->replaceChild($new_node, $html_node);
break;
case 'text':
$html_node->textContent = $new_subject;
break;
}
}
......
......@@ -6,9 +6,9 @@ namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Utility\Html;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
/**
* Tests the dom_str_replace process plugin.
......@@ -62,7 +62,7 @@ final class DomStrReplaceTest extends MigrateProcessTestCase {
],
'mode-invalid' => [
['mode' => 'invalid'],
'Configuration option "mode" only accepts the following values: attribute, element.',
'Configuration option "mode" only accepts the following values: attribute, element, text.',
],
'attribute_options-null' => [
['attribute_options' => NULL],
......@@ -139,6 +139,16 @@ final class DomStrReplaceTest extends MigrateProcessTestCase {
] + self::$exampleConfiguration,
'<a href="/fbar/Fbar/fbar">text</a>',
],
'mode-text' => [
'<a href="/foo/Foo/foo">foo</a>',
[
'mode' => 'text',
'xpath' => '//a',
'search' => 'foo',
'replace' => 'bar',
],
'<a href="/foo/Foo/foo">bar</a>',
],
];
return $cases;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment