Version 1
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Get.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8
9 /**
10  * Gets the source value.
11  *
12  * Available configuration keys:
13  * - source: Source property.
14  *
15  * The get plugin returns the value of the property given by the "source"
16  * configuration key.
17  *
18  * Examples:
19  *
20  * @code
21  * process:
22  *   bar:
23  *     plugin: get
24  *     source: foo
25  * @endcode
26  *
27  * This copies the source value of foo to the destination property "bar".
28  *
29  * Since get is the default process plugin, it can be shorthanded like this:
30  *
31  * @code
32  * process:
33  *   bar: foo
34  * @endcode
35  *
36  * get also supports a list of source properties.
37  *
38  * Example:
39  *
40  * @code
41  * process:
42  *   bar:
43  *     plugin: get
44  *     source:
45  *       - foo1
46  *       - foo2
47  * @endcode
48  *
49  * This copies the array of source values [foo1, foo2] to the destination
50  * property "bar".
51  *
52  * If the list of source properties contains an empty element then the current
53  * value will be used. This makes it impossible to reach a source property with
54  * an empty string as its name.
55  *
56  * get also supports copying destination values. These are indicated by a
57  * starting @ sign. Values using @ must be wrapped in quotes.
58  *
59  * @code
60  * process:
61  *   foo:
62  *     plugin: machine_name
63  *     source: baz
64  *   bar:
65  *     plugin: get
66  *     source: '@foo'
67  * @endcode
68  *
69  * This will simply copy the destination value of foo to the destination
70  * property bar. foo configuration is included for illustration purposes.
71  *
72  * Because of this, if your source or destination property actually starts with
73  * a @ you need to double those starting characters up. This means that if a
74  * destination property happens to start with a @ and you want to refer it,
75  * you'll need to start with three @ characters -- one to indicate the
76  * destination and two for escaping the real @.
77  *
78  * @code
79  * process:
80  * @foo:
81  *   plugin: machine_name
82  *   source: baz
83  * bar:
84  *   plugin: get
85  *   source: '@@@foo'
86  * @endcode
87  *
88  * This should occur extremely rarely.
89  *
90  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
91  *
92  * @MigrateProcessPlugin(
93  *   id = "get"
94  * )
95  */
96 class Get extends ProcessPluginBase {
97
98   /**
99    * Flag indicating whether there are multiple values.
100    *
101    * @var bool
102    */
103   protected $multiple;
104
105   /**
106    * {@inheritdoc}
107    */
108   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
109     $source = $this->configuration['source'];
110     $properties = is_string($source) ? [$source] : $source;
111     $return = [];
112     foreach ($properties as $property) {
113       if ($property || (string) $property === '0') {
114         $is_source = TRUE;
115         if ($property[0] == '@') {
116           $property = preg_replace_callback('/^(@?)((?:@@)*)([^@]|$)/', function ($matches) use (&$is_source) {
117             // If there are an odd number of @ in the beginning, it's a
118             // destination.
119             $is_source = empty($matches[1]);
120             // Remove the possible escaping and do not lose the terminating
121             // non-@ either.
122             return str_replace('@@', '@', $matches[2]) . $matches[3];
123           }, $property);
124         }
125         if ($is_source) {
126           $return[] = $row->getSourceProperty($property);
127         }
128         else {
129           $return[] = $row->getDestinationProperty($property);
130         }
131       }
132       else {
133         $return[] = $value;
134       }
135     }
136
137     if (is_string($source)) {
138       $this->multiple = is_array($return[0]);
139       return $return[0];
140     }
141     return $return;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function multiple() {
148     return $this->multiple;
149   }
150
151 }