class ArrayBuild in Drupal 8
Same name and namespace in other branches
- 9 core/modules/migrate/src/Plugin/migrate/process/ArrayBuild.php \Drupal\migrate\Plugin\migrate\process\ArrayBuild
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate\Plugin\migrate\process\ArrayBuild
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\processView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ArrayBuild:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
1 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |