Chris@4
|
1 <?php
|
Chris@4
|
2
|
Chris@4
|
3 namespace Drupal\migrate_plus\Plugin\migrate\process;
|
Chris@4
|
4
|
Chris@4
|
5 use Drupal\migrate\MigrateException;
|
Chris@4
|
6 use Drupal\migrate\MigrateExecutableInterface;
|
Chris@4
|
7 use Drupal\migrate\ProcessPluginBase;
|
Chris@4
|
8 use Drupal\migrate\Row;
|
Chris@4
|
9
|
Chris@4
|
10 /**
|
Chris@4
|
11 * Uses the str_replace() method on a source string.
|
Chris@4
|
12 *
|
Chris@4
|
13 * @MigrateProcessPlugin(
|
Chris@4
|
14 * id = "str_replace"
|
Chris@4
|
15 * )
|
Chris@4
|
16 *
|
Chris@4
|
17 * To do a simple hardcoded string replace use the following:
|
Chris@4
|
18 *
|
Chris@4
|
19 * @code
|
Chris@4
|
20 * field_text:
|
Chris@4
|
21 * plugin: str_replace
|
Chris@4
|
22 * source: text
|
Chris@4
|
23 * search: foo
|
Chris@4
|
24 * replace: bar
|
Chris@4
|
25 * @endcode
|
Chris@4
|
26 *
|
Chris@4
|
27 * If the value of text is "vero eos et accusam et justo vero" in source, foo is
|
Chris@4
|
28 * "et" in search and bar is "that" in replace, field_text will be "vero eos
|
Chris@4
|
29 * that accusam that justo vero".
|
Chris@4
|
30 *
|
Chris@4
|
31 * Case insensitive searches can be achieved using the following:
|
Chris@4
|
32 * @code
|
Chris@4
|
33 * field_text:
|
Chris@4
|
34 * plugin: str_replace
|
Chris@4
|
35 * case_insensitive: true
|
Chris@4
|
36 * source: text
|
Chris@4
|
37 * search: foo
|
Chris@4
|
38 * replace: bar
|
Chris@4
|
39 * @endcode
|
Chris@4
|
40 *
|
Chris@4
|
41 * If the value of text is "VERO eos et accusam et justo vero" in source, foo is
|
Chris@4
|
42 * "vero" in search and bar is "that" in replace, field_text will be "that eos
|
Chris@4
|
43 * et accusam et justo that".
|
Chris@4
|
44 *
|
Chris@4
|
45 * Also regular expressions can be matched using:
|
Chris@4
|
46 * @code
|
Chris@4
|
47 * field_text:
|
Chris@4
|
48 * plugin: str_replace
|
Chris@4
|
49 * regex: true
|
Chris@4
|
50 * source: text
|
Chris@4
|
51 * search: foo
|
Chris@4
|
52 * replace: bar
|
Chris@4
|
53 * @endcode
|
Chris@4
|
54 *
|
Chris@4
|
55 * If the value of text is "vero eos et 123 accusam et justo 123 duo" in source,
|
Chris@4
|
56 * foo is "/[0-9]{3}/" in search and bar is "the" in replace, field_text will be
|
Chris@4
|
57 * "vero eos et the accusam et justo the duo".
|
Chris@4
|
58 *
|
Chris@4
|
59 * All the rules for
|
Chris@4
|
60 * @link http://php.net/manual/function.str-replace.php str_replace @endlink
|
Chris@4
|
61 * apply. This means that you can provide arrays as values.
|
Chris@4
|
62 *
|
Chris@4
|
63 * Multiple values can be matched like this:
|
Chris@4
|
64 * @code
|
Chris@4
|
65 * field_text:
|
Chris@4
|
66 * plugin: str_replace
|
Chris@4
|
67 * source: text
|
Chris@4
|
68 * search: ["AT", "CH", "DK"]
|
Chris@4
|
69 * replace: ["Austria", "Switzerland", "Denmark"]
|
Chris@4
|
70 * @endcode
|
Chris@4
|
71 */
|
Chris@4
|
72 class StrReplace extends ProcessPluginBase {
|
Chris@4
|
73
|
Chris@4
|
74 /**
|
Chris@4
|
75 * Flag indicating whether there are multiple values.
|
Chris@4
|
76 *
|
Chris@4
|
77 * @var bool
|
Chris@4
|
78 */
|
Chris@4
|
79 protected $multiple;
|
Chris@4
|
80
|
Chris@4
|
81 /**
|
Chris@4
|
82 * {@inheritdoc}
|
Chris@4
|
83 */
|
Chris@4
|
84 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
Chris@4
|
85 if (!isset($this->configuration['search'])) {
|
Chris@4
|
86 throw new MigrateException('"search" must be configured.');
|
Chris@4
|
87 }
|
Chris@4
|
88 if (!isset($this->configuration['replace'])) {
|
Chris@4
|
89 throw new MigrateException('"replace" must be configured.');
|
Chris@4
|
90 }
|
Chris@4
|
91 $this->multiple = is_array($value);
|
Chris@4
|
92 $this->configuration += [
|
Chris@4
|
93 'case_insensitive' => FALSE,
|
Chris@4
|
94 'regex' => FALSE,
|
Chris@4
|
95 ];
|
Chris@4
|
96 $function = "str_replace";
|
Chris@4
|
97 if ($this->configuration['case_insensitive']) {
|
Chris@4
|
98 $function = 'str_ireplace';
|
Chris@4
|
99 }
|
Chris@4
|
100 if ($this->configuration['regex']) {
|
Chris@4
|
101 $function = 'preg_replace';
|
Chris@4
|
102 }
|
Chris@4
|
103 return $function($this->configuration['search'], $this->configuration['replace'], $value);
|
Chris@4
|
104 }
|
Chris@4
|
105
|
Chris@4
|
106 /**
|
Chris@4
|
107 * {@inheritdoc}
|
Chris@4
|
108 */
|
Chris@4
|
109 public function multiple() {
|
Chris@4
|
110 return $this->multiple;
|
Chris@4
|
111 }
|
Chris@4
|
112
|
Chris@4
|
113 }
|