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