10b6eca20868a7a1ac4a0a521d618f1bbd81967c
[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 the source or destination property actually starts with a
73  * "@", that character must be escaped with "@@". The referenced property
74  * becomes, for example, "@@@foo".
75  *
76  * @code
77  * process:
78  *   '@foo':
79  *     plugin: machine_name
80  *     source: baz
81  *   bar:
82  *     plugin: get
83  *     source: '@@@foo'
84  * @endcode
85  *
86  * This should occur extremely rarely.
87  *
88  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
89  *
90  * @MigrateProcessPlugin(
91  *   id = "get"
92  * )
93  */
94 class Get extends ProcessPluginBase {
95
96   /**
97    * Flag indicating whether there are multiple values.
98    *
99    * @var bool
100    */
101   protected $multiple;
102
103   /**
104    * {@inheritdoc}
105    */
106   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
107     $source = $this->configuration['source'];
108     $properties = is_string($source) ? [$source] : $source;
109     $return = [];
110     foreach ($properties as $property) {
111       if ($property || (string) $property === '0') {
112         $is_source = TRUE;
113         if ($property[0] == '@') {
114           $property = preg_replace_callback('/^(@?)((?:@@)*)([^@]|$)/', function ($matches) use (&$is_source) {
115             // If there are an odd number of @ in the beginning, it's a
116             // destination.
117             $is_source = empty($matches[1]);
118             // Remove the possible escaping and do not lose the terminating
119             // non-@ either.
120             return str_replace('@@', '@', $matches[2]) . $matches[3];
121           }, $property);
122         }
123         if ($is_source) {
124           $return[] = $row->getSourceProperty($property);
125         }
126         else {
127           $return[] = $row->getDestinationProperty($property);
128         }
129       }
130       else {
131         $return[] = $value;
132       }
133     }
134
135     if (is_string($source)) {
136       $this->multiple = is_array($return[0]);
137       return $return[0];
138     }
139     return $return;
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function multiple() {
146     return $this->multiple;
147   }
148
149 }