3e3d8dcec5ac4737fb72c73d0986cc8b9cdb4d96
[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  * Examples:
22  *
23  * @code
24  * source: Array
25  * (
26  *   [upload] => Array
27  *     (
28  *       [0] => Array
29  *         (
30  *           [fid] => 1
31  *           [list] => 0
32  *           [description] => "File number 1"
33  *         )
34  *       [1] => Array
35  *         (
36  *           [fid] => 2
37  *           [list] => 1
38  *           [description] => "File number 2"
39  *         )
40  *     )
41  * )
42  * ...
43  * @endcode
44  *
45  * The sub_process process plugin will take these arrays one at a time and run
46  * its own process over each one:
47  *
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  *
62  * In this case, each item in the upload array will be processed by the
63  * sub_process process plugin. The target_id will be found by looking up the
64  * destination value from a previous migration. The display and description
65  * fields will simply be mapped.
66  *
67  * In the next example, normally the array returned from sub_process will have
68  * its original keys. If you need to change the key, it is possible for the
69  * returned array to be keyed by one of the transformed values in the sub-array.
70  *
71  * @code
72  * source: Array
73  * (
74  *   [format] => 1
75  *   [name] => Filtered HTML
76  * ...
77  *   [filters] => Array
78  *     (
79  *       [0] => Array
80  *         (
81  *           [module] => filter
82  *           [delta] => 2
83  *           [weight] => 0
84  *         )
85  *       [1] => Array
86  *         (
87  *           [module] => filter
88  *           [delta] => 0
89  *           [weight] => 1
90  *         )
91  *    )
92  * )
93  * ...
94  *
95  * process:
96  *   filters:
97  *     plugin: sub_process
98  *     source: filters
99  *     key: "@id"
100  *     process:
101  *       id:
102  *         plugin: concat
103  *         source:
104  *           - module
105  *           - delta
106  *         delimiter: _
107  * @endcode
108  *
109  * In the above example, the keys of the returned array would be filter_2 and
110  * filter_0
111  *
112  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
113  *
114  * @MigrateProcessPlugin(
115  *   id = "sub_process",
116  *   handle_multiples = TRUE
117  * )
118  */
119 class SubProcess extends ProcessPluginBase {
120
121   /**
122    * {@inheritdoc}
123    */
124   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
125     $return = [];
126     if (is_array($value) || $value instanceof \Traversable) {
127       foreach ($value as $key => $new_value) {
128         $new_row = new Row($new_value, []);
129         $migrate_executable->processRow($new_row, $this->configuration['process']);
130         $destination = $new_row->getDestination();
131         if (array_key_exists('key', $this->configuration)) {
132           $key = $this->transformKey($key, $migrate_executable, $new_row);
133         }
134         $return[$key] = $destination;
135       }
136     }
137     return $return;
138   }
139
140   /**
141    * Runs the process pipeline for the key to determine its dynamic name.
142    *
143    * @param string|int $key
144    *   The current key.
145    * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
146    *   The migrate executable helper class.
147    * @param \Drupal\migrate\Row $row
148    *   The current row after processing.
149    *
150    * @return mixed
151    *   The transformed key.
152    */
153   protected function transformKey($key, MigrateExecutableInterface $migrate_executable, Row $row) {
154     $process = ['key' => $this->configuration['key']];
155     $migrate_executable->processRow($row, $process, $key);
156     return $row->getDestinationProperty('key');
157   }
158
159   /**
160    * {@inheritdoc}
161    */
162   public function multiple() {
163     return TRUE;
164   }
165
166 }