Pull merge.
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Extract.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
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;
10
11 /**
12  * Extracts a value from an array.
13  *
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
18  * 'value'.
19  *
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
24  *   key does not exist.
25  *
26  * Examples:
27  *
28  * @code
29  * process:
30  *   new_text_field:
31  *     plugin: extract
32  *     source: some_text_field
33  *     index:
34  *       - und
35  *       - 0
36  *       - value
37  * @endcode
38  *
39  * The PHP equivalent of this would be:
40  * @code
41  * $destination['new_text_field'] = $source['some_text_field']['und'][0]['value'];
42  * @endcode
43  * If a default value is specified, it will be returned if the index does not
44  * exist in the input array.
45  *
46  * @code
47  * plugin: extract
48  * source: some_text_field
49  * default: 'Default title'
50  * index:
51  *   - title
52  * @endcode
53  *
54  * If $source['some_text_field']['title'] doesn't exist, then the plugin will
55  * return "Default title".
56  *
57  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
58  *
59  * @MigrateProcessPlugin(
60  *   id = "extract",
61  *   handle_multiples = TRUE
62  * )
63  */
64 class Extract extends ProcessPluginBase {
65
66   /**
67    * {@inheritdoc}
68    */
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.');
72     }
73     $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists);
74     if (!$key_exists) {
75       if (isset($this->configuration['default'])) {
76         $new_value = $this->configuration['default'];
77       }
78       else {
79         throw new MigrateException('Array index missing, extraction failed.');
80       }
81     }
82     return $new_value;
83   }
84
85 }