Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Flatten.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4 use Drupal\migrate\MigrateExecutableInterface;
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\Row;
7
8 /**
9  * Flattens the source value.
10  *
11  * The flatten process plugin converts a nested array into a flat array. For
12  * example [[1, 2, [3, 4]], [5], 6] becomes [1, 2, 3, 4, 5, 6]. During some
13  * types of processing (e.g. user permission splitting), what was once a
14  * one-dimensional array gets transformed into a multidimensional array. This
15  * plugin will flatten them back down to one-dimensional arrays again.
16  *
17  * Example:
18  *
19  * @code
20  * process:
21  *   tags:
22  *      -
23  *        plugin: default_value
24  *        source: foo
25  *        default_value: [bar, [qux, quux]]
26  *      -
27  *        plugin: flatten
28  * @endcode
29  *
30  * In this example, the default_value process returns [bar, [qux, quux]] (given
31  * a NULL value of foo). At this point, Migrate would try to import two
32  * items: bar and [qux, quux]. The latter is not a valid one and won't be
33  * imported. We need to pass the values through the flatten processor to obtain
34  * a three items array [bar, qux, quux], suitable for import.
35  *
36  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
37  *
38  * @MigrateProcessPlugin(
39  *   id = "flatten",
40  *   handle_multiples = TRUE
41  * )
42  */
43 class Flatten extends ProcessPluginBase {
44
45   /**
46    * Flatten nested array values to single array values.
47    *
48    * For example, [[1, 2, [3, 4]]] becomes [1, 2, 3, 4].
49    */
50   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
51     return iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($value)), FALSE);
52   }
53
54 }