3 namespace Drupal\migrate_plus\Plugin\migrate\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\ProcessPluginBase;
8 use Drupal\migrate\Row;
11 * Uses the str_replace() method on a source string.
13 * @MigrateProcessPlugin(
17 * To do a simple hardcoded string replace use the following:
27 * If the value of text is "vero eos et accusam et justo vero" in source, foo is
28 * "et" in search and bar is "that" in replace, field_text will be "vero eos
29 * that accusam that justo vero".
31 * Case insensitive searches can be achieved using the following:
35 * case_insensitive: true
41 * If the value of text is "VERO eos et accusam et justo vero" in source, foo is
42 * "vero" in search and bar is "that" in replace, field_text will be "that eos
43 * et accusam et justo that".
45 * Also regular expressions can be matched using:
55 * If the value of text is "vero eos et 123 accusam et justo 123 duo" in source,
56 * foo is "/[0-9]{3}/" in search and bar is "the" in replace, field_text will be
57 * "vero eos et the accusam et justo the duo".
60 * @link http://php.net/manual/function.str-replace.php str_replace @endlink
61 * apply. This means that you can provide arrays as values.
63 class StrReplace extends ProcessPluginBase {
66 * Flag indicating whether there are multiple values.
75 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
76 if (!isset($this->configuration['search'])) {
77 throw new MigrateException('"search" must be configured.');
79 if (!isset($this->configuration['replace'])) {
80 throw new MigrateException('"replace" must be configured.');
82 $this->multiple = is_array($value);
83 $this->configuration += [
84 'case_insensitive' => FALSE,
87 $function = "str_replace";
88 if ($this->configuration['case_insensitive']) {
89 $function = 'str_ireplace';
91 if ($this->configuration['regex']) {
92 $function = 'preg_replace';
94 return $function($this->configuration['search'], $this->configuration['replace'], $value);
100 public function multiple() {
101 return $this->multiple;