class DomStrReplace in Migrate Plus 8.5
Same name and namespace in other branches
- 8.4 src/Plugin/migrate/process/DomStrReplace.php \Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace
String replacements on a source dom.
Analogous to str_replace process plugin, but based on a \DOMDocument instead of a string. Meant to be used after dom process plugin.
Available configuration keys:
- mode: What to modify. Possible values:
- attribute: One element attribute.
- element: An element name.
- 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:
- name: Name of the attribute to match and modify.
- search: pattern to match.
- replace: value to replace the searched pattern with.
- regex: Use regular expression replacement.
- case_insensitive: Case insensitive search. Only valid when regex is false.
Examples:
process:
'body/value':
-
plugin: dom
method: import
source: 'body/0/value'
-
plugin: dom_str_replace
mode: attribute
xpath: '//a'
attribute_options:
name: href
search: 'foo'
replace: 'bar'
-
plugin: dom_str_replace
mode: attribute
xpath: '//a'
attribute_options:
name: href
regex: true
search: '/foo/'
replace: 'bar'
-
plugin: dom_str_replace
mode: element
xpath: '//b'
search: 'b'
replace: 'strong'
-
plugin: dom
method: export
Plugin annotation
@MigrateProcessPlugin(
id = "dom_str_replace"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\DomProcessBase
- class \Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace
- class \Drupal\migrate_plus\Plugin\migrate\process\DomProcessBase
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DomStrReplace
1 file declares its use of DomStrReplace
- DomStrReplaceTest.php in tests/
src/ Unit/ process/ DomStrReplaceTest.php
File
- src/
Plugin/ migrate/ process/ DomStrReplace.php, line 70
Namespace
Drupal\migrate_plus\Plugin\migrate\processView source
class DomStrReplace extends DomProcessBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration += [
'case_insensitive' => FALSE,
'regex' => FALSE,
];
$options_validation = [
'xpath' => NULL,
'mode' => [
'attribute' => [
'attribute_options' => NULL,
],
'element' => [],
],
'search' => NULL,
'replace' => NULL,
];
foreach ($options_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required.");
}
if (!empty($possible_values) && !array_key_exists($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', array_keys($possible_values))));
}
}
$mode = $this->configuration['mode'];
$mode_validation = $options_validation['mode'][$mode];
foreach ($mode_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required for mode '{$mode}'.");
}
if (!is_null($possible_values) && !in_array($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', $possible_values)));
}
}
$mode_validation = $options_validation['mode'][$this->configuration['mode']];
foreach ($mode_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required for mode '{$mode}'.");
}
if (!is_null($possible_values) && !in_array($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', $possible_values)));
}
}
$mode_validation = $options_validation['mode'][$this->configuration['mode']];
foreach ($mode_validation as $option_name => $possible_values) {
if (empty($this->configuration[$option_name])) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), "Configuration option '{$option_name}' is required for mode '{$mode}'.");
}
if (!is_null($possible_values) && !in_array($this->configuration[$option_name], $possible_values)) {
throw new InvalidPluginDefinitionException($this
->getPluginId(), sprintf('Configuration option "%s" only accepts the following values: %s.', $option_name, implode(', ', $possible_values)));
}
}
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$this
->init($value, $destination_property);
foreach ($this->xpath
->query($this->configuration['xpath']) as $html_node) {
$subject = $this
->getSubject($html_node);
if (empty($subject)) {
// Could not find subject, skip processing.
continue;
}
$search = $this
->getSearch();
$replace = $this
->getReplace();
$this
->doReplace($html_node, $search, $replace, $subject);
}
return $this->document;
}
/**
* Retrieves the right subject string.
*
* @param \DOMElement $node
* The current element from iteration.
*
* @return string
* The string to use a subject on search.
*/
protected function getSubject(\DOMElement $node) {
switch ($this->configuration['mode']) {
case 'attribute':
return $node
->getAttribute($this->configuration['attribute_options']['name']);
case 'element':
return $node->nodeName;
}
}
/**
* Retrieves the right search string based on configuration.
*
* @return string
* The value to be searched.
*/
protected function getSearch() {
switch ($this->configuration['mode']) {
case 'attribute':
case 'element':
return $this->configuration['search'];
}
}
/**
* Retrieves the right replace string based on configuration.
*
* @return string
* The value to use for replacement.
*/
protected function getReplace() {
switch ($this->configuration['mode']) {
case 'attribute':
case 'element':
return $this->configuration['replace'];
}
}
/**
* Retrieves the right replace string based on configuration.
*
* @param \DOMElement $html_node
* The current element from iteration.
* @param string $search
* The search string or pattern.
* @param string $replace
* The replacement string.
* @param string $subject
* The string on which to perform the substitution.
*/
protected function doReplace(\DOMElement $html_node, $search, $replace, $subject) {
if ($this->configuration['regex']) {
$function = 'preg_replace';
}
elseif ($this->configuration['case_insensitive']) {
$function = 'str_ireplace';
}
else {
$function = 'str_replace';
}
$new_subject = $function($search, $replace, $subject);
$this
->postReplace($html_node, $new_subject);
}
/**
* Performs post-replace actions.
*
* @param \DOMElement $html_node
* The current element from iteration.
* @param string $new_subject
* The new value to use.
*/
protected function postReplace(\DOMElement $html_node, $new_subject) {
switch ($this->configuration['mode']) {
case 'attribute':
$html_node
->setAttribute($this->configuration['attribute_options']['name'], $new_subject);
break;
case 'element':
$new_node = $this->document
->createElement($new_subject);
foreach ($html_node->childNodes as $child) {
$new_node
->appendChild($child
->cloneNode(TRUE));
}
foreach ($html_node->attributes as $attribute) {
$new_node
->setAttribute($attribute->name, $attribute->value);
}
$html_node->parentNode
->replaceChild($new_node, $html_node);
break;
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomProcessBase:: |
protected | property | Document to use. | |
DomProcessBase:: |
protected | property | Xpath query object. | |
DomProcessBase:: |
protected | function | Initialize the class properties. | |
DomStrReplace:: |
protected | function | Retrieves the right replace string based on configuration. | 1 |
DomStrReplace:: |
protected | function | Retrieves the right replace string based on configuration. | |
DomStrReplace:: |
protected | function | Retrieves the right search string based on configuration. | |
DomStrReplace:: |
protected | function | Retrieves the right subject string. | |
DomStrReplace:: |
protected | function | Performs post-replace actions. | |
DomStrReplace:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
1 |
DomStrReplace:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |