3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\ProcessPluginBase;
8 use Drupal\migrate\Row;
11 * Builds an array based on the key and value configuration.
13 * The array_build plugin builds a single associative array by extracting keys
14 * and values from each array in the input value, which is expected to be an
15 * array of arrays. The keys of the returned array will be determined by the
16 * 'key' configuration option, and the values will be determined by the 'value'
19 * Available configuration keys
20 * - key: The key used to lookup a value in the source arrays to be used as
21 * a key in the destination array.
22 * - value: The key used to lookup a value in the source arrays to be used as
23 * a value in the destination array.
27 * Consider the migration of language negotiation by domain.
28 * The source is an array of all the languages:
37 * [domain] => http://example.com
43 * [domain] => http://fr.example.com
48 * The destination should be an array of all the domains keyed by their
54 * [en] => http://example.com
55 * [fr] => http://fr.example.com
59 * The array_build process plugin would be used like this:
70 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
72 * @MigrateProcessPlugin(
74 * handle_multiples = TRUE
77 class ArrayBuild extends ProcessPluginBase {
82 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
85 foreach ((array) $value as $old_key => $old_value) {
86 // Checks that $old_value is an array.
87 if (!is_array($old_value)) {
88 throw new MigrateException("The input should be an array of arrays");
91 // Checks that the key exists.
92 if (!array_key_exists($this->configuration['key'], $old_value)) {
93 throw new MigrateException("The key '" . $this->configuration['key'] . "' does not exist");
96 // Checks that the value exists.
97 if (!array_key_exists($this->configuration['value'], $old_value)) {
98 throw new MigrateException("The key '" . $this->configuration['value'] . "' does not exist");
101 $new_value[$old_value[$this->configuration['key']]] = $old_value[$this->configuration['value']];