Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / SubProcess.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  * Runs an array of arrays through its own process pipeline.
11  *
12  * The sub_process plugin accepts an array of associative arrays and runs each
13  * one through its own process pipeline, producing a newly keyed associative
14  * array of transformed values.
15  *
16  * Available configuration keys:
17  *   - process: the plugin(s) that will process each element of the source.
18  *   - key: runs the process pipeline for the key to determine a new dynamic
19  *     name.
20  *
21  * Example 1:
22  *
23  * This example demonstrates how migration_lookup process plugin can be applied
24  * on the following source data.
25  * @code
26  * source: Array
27  * (
28  *   [upload] => Array
29  *     (
30  *       [0] => Array
31  *         (
32  *           [fid] => 1
33  *           [list] => 0
34  *           [description] => "File number 1"
35  *         )
36  *       [1] => Array
37  *         (
38  *           [fid] => 2
39  *           [list] => 1
40  *           [description] => "File number 2"
41  *         )
42  *     )
43  * )
44  * ...
45  * @endcode
46  * The sub_process process plugin will take these arrays one at a time and run
47  * its own process for each of them:
48  * @code
49  * process:
50  *   upload:
51  *     plugin: sub_process
52  *     source: upload
53  *     process:
54  *       target_id:
55  *         plugin: migration_lookup
56  *         migration: d6_file
57  *         source: fid
58  *       display: list
59  *       description: description
60  * @endcode
61  * In this case, each item in the upload array will be processed by the
62  * sub_process process plugin. The target_id will be found by looking up the
63  * destination value from a previous migration using the migration_lookup
64  * process plugin. The display and description fields will be mapped directly.
65  *
66  * Example 2.
67  *
68  * Drupal 6 filter formats contain a list of filters belonging to that format
69  * identified by a numeric delta. A delta of 1 indicates automatic linebreaks,
70  * delta of 2 indicates the URL filter and so on. This example demonstrates how
71  * static_map process plugin can be applied on the following source data.
72  * @code
73  * source: Array
74  * (
75  *   [format] => 1
76  *   [name] => Filtered HTML
77  * ...
78  *   [filters] => Array
79  *     (
80  *       [0] => Array
81  *         (
82  *           [module] => filter
83  *           [delta] => 2
84  *           [weight] => 0
85  *         )
86  *       [1] => Array
87  *         (
88  *           [module] => filter
89  *           [delta] => 0
90  *           [weight] => 1
91  *         )
92  *    )
93  * )
94  * ...
95  * @endcode
96  * The sub_process will take these arrays one at a time and run its own process
97  * for each of them:
98  * @code
99  * process:
100  *   filters:
101  *     plugin: sub_process
102  *     source: filters
103  *     process:
104  *       id:
105  *         plugin: static_map
106  *         source:
107  *           - module
108  *           - delta
109  *         map:
110  *           filter:
111  *             0: filter_html_escape
112  *             1: filter_autop
113  *             2: filter_url
114  *             3: filter_htmlcorrector
115  *             4: filter_html_escape
116  *           php:
117  *             0: php_code
118  * @endcode
119  * The example above means that we take each array element ([0], [1], etc.) from
120  * the source filters field and apply the static_map plugin on it. Let's have a
121  * closer look at the first array at index 0:
122  * @code
123  * Array
124  * (
125  *    [module] => filter
126  *    [delta] => 2
127  *    [weight] => 0
128  * )
129  * @endcode
130  * The static_map process plugin results to value 'filter_url' for this input
131  * based on the 'module' and 'delta' map.
132  *
133  * Example 3.
134  *
135  * Normally the array returned from sub_process will have its original keys. If
136  * you need to change the key, it is possible for the returned array to be keyed
137  * by one of the transformed values in the sub-array. For the same source data
138  * used in the previous example, the migration below would result to keys
139  * 'filter_2' and 'filter_0'.
140  * @code
141  * process:
142  *   filters:
143  *     plugin: sub_process
144  *     source: filters
145  *     key: "@id"
146  *     process:
147  *       id:
148  *         plugin: concat
149  *         source:
150  *           - module
151  *           - delta
152  *         delimiter: _
153  * @endcode
154  *
155  * @see \Drupal\migrate\Plugin\migrate\process\MigrationLookup
156  * @see \Drupal\migrate\Plugin\migrate\process\StaticMap
157  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
158  *
159  * @MigrateProcessPlugin(
160  *   id = "sub_process",
161  *   handle_multiples = TRUE
162  * )
163  */
164 class SubProcess extends ProcessPluginBase {
165
166   /**
167    * {@inheritdoc}
168    */
169   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
170     $return = [];
171     if (is_array($value) || $value instanceof \Traversable) {
172       foreach ($value as $key => $new_value) {
173         $new_row = new Row($new_value, []);
174         $migrate_executable->processRow($new_row, $this->configuration['process']);
175         $destination = $new_row->getDestination();
176         if (array_key_exists('key', $this->configuration)) {
177           $key = $this->transformKey($key, $migrate_executable, $new_row);
178         }
179         $return[$key] = $destination;
180       }
181     }
182     return $return;
183   }
184
185   /**
186    * Runs the process pipeline for the key to determine its dynamic name.
187    *
188    * @param string|int $key
189    *   The current key.
190    * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
191    *   The migrate executable helper class.
192    * @param \Drupal\migrate\Row $row
193    *   The current row after processing.
194    *
195    * @return mixed
196    *   The transformed key.
197    */
198   protected function transformKey($key, MigrateExecutableInterface $migrate_executable, Row $row) {
199     $process = ['key' => $this->configuration['key']];
200     $migrate_executable->processRow($row, $process, $key);
201     return $row->getDestinationProperty('key');
202   }
203
204   /**
205    * {@inheritdoc}
206    */
207   public function multiple() {
208     return TRUE;
209   }
210
211 }