You are here

class ArrayBuild in Drupal 8

Same name and namespace 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

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

Namesort descending Modifiers Type Description Overrides
ArrayBuild::transform public function Performs the associated process. Overrides ProcessPluginBase::transform 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.