Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / process / Merge.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate\process;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Row;
9
10 /**
11  * This plugin merges arrays together.
12  *
13  * @MigrateProcessPlugin(
14  *   id = "merge"
15  * )
16  *
17  * Use to merge several fields into one. In the following example, imagine a D7
18  * node with a field_collections field and an image field that migrations were
19  * written for to make paragraph entities in D8. We would like to add those
20  * paragraph entities to the 'paragraphs_field'. Consider the following:
21  *
22  *  source:
23  *    plugin: d7_node
24  *  process:
25  *    temp_body:
26  *      plugin: iterator
27  *      source: field_section
28  *      process:
29  *        target_id:
30  *          plugin: migration_lookup
31  *          migration: field_collection_field_section_to_paragraph
32  *          source: value
33  *    temp_images:
34  *      plugin: iterator
35  *      source: field_image
36  *      process
37  *        target_id:
38  *          plugin: migration_lookup
39  *          migration: image_entities_to_paragraph
40  *          source: fid
41  *    paragraphs_field:
42  *      plugin: merge
43  *      source:
44  *        - '@temp_body'
45  *        - '@temp_images'
46  *  destination:
47  *    plugin: 'entity:node'
48  */
49 class Merge extends ProcessPluginBase {
50
51   /**
52    * {@inheritdoc}
53    */
54   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
55     if (!is_array($value)) {
56       throw new MigrateException(sprintf('Merge process failed for destination property (%s): input is not an array.', $destination_property));
57     }
58     $new_value = [];
59     foreach($value as $i => $item) {
60       if (!is_array($item)) {
61         throw new MigrateException(sprintf('Merge process failed for destination property (%s): index (%s) in the source value is not an array that can be merged.', $destination_property, $i));
62       }
63       $new_value = array_merge($new_value, $item);
64     }
65     return $new_value;
66   }
67
68 }