3 namespace Drupal\migrate\Plugin\migrate\process;
4 use Drupal\migrate\MigrateExecutableInterface;
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\Row;
9 * Flattens the source value.
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.
23 * plugin: default_value
25 * default_value: [bar, [qux, quux]]
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.
36 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
38 * @MigrateProcessPlugin(
40 * handle_multiples = TRUE
43 class Flatten extends ProcessPluginBase {
46 * Flatten nested array values to single array values.
48 * For example, [[1, 2, [3, 4]]] becomes [1, 2, 3, 4].
50 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
51 return iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($value)), FALSE);