class ArrayBuild

Same name in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/ArrayBuild.php \Drupal\migrate\Plugin\migrate\process\ArrayBuild
  2. 10 core/modules/migrate/src/Plugin/migrate/process/ArrayBuild.php \Drupal\migrate\Plugin\migrate\process\ArrayBuild
  3. 11.x core/modules/migrate/src/Plugin/migrate/process/ArrayBuild.php \Drupal\migrate\Plugin\migrate\process\ArrayBuild

Builds an array based on the key and value configuration.

The array_build plugin builds a single associative array by extracting keys and values from each array in the input value, which is expected to be an array of arrays. The keys of the returned array will be determined by the 'key' configuration option, and the values will be determined by the 'value' option.

Available configuration keys

  • key: The key used to lookup a value in the source arrays to be used as a key in the destination array.
  • value: The key used to lookup a value in the source arrays to be used as a value in the destination array.

Example:

Consider the migration of language negotiation by domain. The source is an array of all the languages:


languages: Array
(
  [0] => Array
    (
      [language] => en
...
      [domain] => http://example.com
    )
  [1] => Array
    (
      [language] => fr
...
      [domain] => http://fr.example.com
    )
...

The destination should be an array of all the domains keyed by their language code:


domains: Array
(
  [en] => http://example.com
  [fr] => http://fr.example.com
...

The array_build process plugin would be used like this:


process:
  domains:
    plugin: array_build
    key: language
    value: domain
    source: languages

Plugin annotation


@MigrateProcessPlugin(
  id = "array_build",
  handle_multiples = TRUE
)

Hierarchy

Expanded class hierarchy of ArrayBuild

See also

\Drupal\migrate\Plugin\MigrateProcessInterface

2 files declare their use of ArrayBuild
ArrayBuildTest.php in core/modules/migrate/tests/src/Unit/process/ArrayBuildTest.php
LanguageDomains.php in core/modules/language/src/Plugin/migrate/process/LanguageDomains.php

File

core/modules/migrate/src/Plugin/migrate/process/ArrayBuild.php, line 77

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class ArrayBuild extends ProcessPluginBase {
    
    /**
     * {@inheritdoc}
     */
    public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
        $new_value = [];
        foreach ((array) $value as $old_key => $old_value) {
            // Checks that $old_value is an array.
            if (!is_array($old_value)) {
                throw new MigrateException("The input should be an array of arrays");
            }
            // Checks that the key exists.
            if (!array_key_exists($this->configuration['key'], $old_value)) {
                throw new MigrateException("The key '" . $this->configuration['key'] . "' does not exist");
            }
            // Checks that the value exists.
            if (!array_key_exists($this->configuration['value'], $old_value)) {
                throw new MigrateException("The key '" . $this->configuration['value'] . "' does not exist");
            }
            $new_value[$old_value[$this->configuration['key']]] = $old_value[$this->configuration['value']];
        }
        return $new_value;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ArrayBuild::transform public function Performs the associated process. Overrides ProcessPluginBase::transform 1
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.