3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\migrate\MigrateExecutableInterface;
6 use Drupal\migrate\ProcessPluginBase;
7 use Drupal\migrate\Row;
10 * Flattens the source value.
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.
24 * plugin: default_value
26 * default_value: [bar, [qux, quux]]
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.
37 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
39 * @MigrateProcessPlugin(
41 * handle_multiples = TRUE
44 class Flatten extends ProcessPluginBase {
47 * Flatten nested array values to single array values.
49 * For example, [[1, 2, [3, 4]]] becomes [1, 2, 3, 4].
51 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
52 return iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($value)), FALSE);