class StrReplace in Migrate Plus 8.5
Same name and namespace in other branches
- 8.4 src/Plugin/migrate/process/StrReplace.php \Drupal\migrate_plus\Plugin\migrate\process\StrReplace
Uses the str_replace() method on a source string.
@MigrateProcessPlugin( id = "str_replace" )
@codingStandardsIgnoreStart
To do a simple hardcoded string replace, use the following:
field_text:
plugin: str_replace
source: text
search: foo
replace: bar
If the value of text is "vero eos et accusam et justo vero" in source, foo is "et" in search and bar is "that" in replace, field_text will be "vero eos that accusam that justo vero".
Case insensitive searches can be achieved using the following:
field_text:
plugin: str_replace
case_insensitive: true
source: text
search: foo
replace: bar
If the value of text is "VERO eos et accusam et justo vero" in source, foo is "vero" in search and bar is "that" in replace, field_text will be "that eos et accusam et justo that".
Also regular expressions can be matched using:
field_text:
plugin: str_replace
regex: true
source: text
search: foo
replace: bar
If the value of text is "vero eos et 123 accusam et justo 123 duo" in source, foo is "/[0-9]{3}/" in search and bar is "the" in replace, field_text will be "vero eos et the accusam et justo the duo".
All the rules for str_replace apply. This means that you can provide arrays as values.
Multiple values can be matched like this:
field_text:
plugin: str_replace
source: text
search: ["AT", "CH", "DK"]
replace: ["Austria", "Switzerland", "Denmark"]
@codingStandardsIgnoreEnd
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\StrReplace
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of StrReplace
1 file declares its use of StrReplace
- StrReplaceTest.php in tests/
src/ Unit/ process/ StrReplaceTest.php
File
- src/
Plugin/ migrate/ process/ StrReplace.php, line 72
Namespace
Drupal\migrate_plus\Plugin\migrate\processView source
class StrReplace extends ProcessPluginBase {
/**
* Flag indicating whether there are multiple values.
*
* @var bool
*/
protected $multiple;
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!isset($this->configuration['search'])) {
throw new MigrateException('"search" must be configured.');
}
if (!isset($this->configuration['replace'])) {
throw new MigrateException('"replace" must be configured.');
}
$this->multiple = is_array($value);
$this->configuration += [
'case_insensitive' => FALSE,
'regex' => FALSE,
];
$function = 'str_replace';
if ($this->configuration['case_insensitive']) {
$function = 'str_ireplace';
}
if ($this->configuration['regex']) {
$function = 'preg_replace';
}
return $function($this->configuration['search'], $this->configuration['replace'], $value);
}
/**
* {@inheritdoc}
*/
public function multiple() {
return $this->multiple;
}
}
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 | |
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
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. | |
StrReplace:: |
protected | property | Flag indicating whether there are multiple values. | |
StrReplace:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides ProcessPluginBase:: |
|
StrReplace:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |