3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\migrate\ProcessPluginBase;
7 use Drupal\migrate\MigrateException;
8 use Drupal\migrate\MigrateExecutableInterface;
9 use Drupal\migrate\Row;
12 * Extracts a value from an array.
14 * The extract process plugin is used to pull data from an input array, which
15 * may have multiple levels. One use case is extracting data from field arrays
16 * in previous versions of Drupal. For instance, in Drupal 7, a field array
17 * would be indexed first by language, then by delta, then finally a key such as
20 * Available configuration keys:
21 * - source: The input value - must be an array.
22 * - index: The array of keys to access the value.
23 * - default: (optional) A default value to assign to the destination if the
32 * source: some_text_field
39 * The PHP equivalent of this would be:
41 * $destination['new_text_field'] = $source['some_text_field']['und'][0]['value'];
43 * If a default value is specified, it will be returned if the index does not
44 * exist in the input array.
48 * source: some_text_field
49 * default: 'Default title'
54 * If $source['some_text_field']['title'] doesn't exist, then the plugin will
55 * return "Default title".
57 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
59 * @MigrateProcessPlugin(
61 * handle_multiples = TRUE
64 class Extract extends ProcessPluginBase {
69 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
70 if (!is_array($value)) {
71 throw new MigrateException('Input should be an array.');
73 $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists);
75 if (isset($this->configuration['default'])) {
76 $new_value = $this->configuration['default'];
79 throw new MigrateException('Array index missing, extraction failed.');