d85abfdb3b8ded1e09fc4d61bd0c2e0ca0987b2e
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Iterator.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8
9 /**
10  * This plugin iterates and processes an array.
11  *
12  * @link https://www.drupal.org/node/2135345 Online handbook documentation for iterator process plugin @endlink
13  *
14  * @MigrateProcessPlugin(
15  *   id = "iterator",
16  *   handle_multiples = TRUE
17  * )
18  */
19 class Iterator extends ProcessPluginBase {
20
21   /**
22    * Runs a process pipeline on each destination property per list item.
23    */
24   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
25     $return = [];
26     if (!is_null($value)) {
27       foreach ($value as $key => $new_value) {
28         $new_row = new Row($new_value, []);
29         $migrate_executable->processRow($new_row, $this->configuration['process']);
30         $destination = $new_row->getDestination();
31         if (array_key_exists('key', $this->configuration)) {
32           $key = $this->transformKey($key, $migrate_executable, $new_row);
33         }
34         $return[$key] = $destination;
35       }
36     }
37     return $return;
38   }
39
40   /**
41    * Runs the process pipeline for the current key.
42    *
43    * @param string|int $key
44    *   The current key.
45    * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
46    *   The migrate executable helper class.
47    * @param \Drupal\migrate\Row $row
48    *   The current row after processing.
49    *
50    * @return mixed
51    *   The transformed key.
52    */
53   protected function transformKey($key, MigrateExecutableInterface $migrate_executable, Row $row) {
54     $process = ['key' => $this->configuration['key']];
55     $migrate_executable->processRow($row, $process, $key);
56     return $row->getDestinationProperty('key');
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function multiple() {
63     return TRUE;
64   }
65
66 }