You are here

class StrReplace in Migrate Plus 8.4

Same name and namespace in other branches
  1. 8.5 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

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\process
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
StrReplace::$multiple protected property Flag indicating whether there are multiple values.
StrReplace::multiple public function Indicates whether the returned value requires multiple handling. Overrides ProcessPluginBase::multiple
StrReplace::transform public function Performs the associated process. Overrides ProcessPluginBase::transform