My thought here is that there are situations where I would like to merge two fields that are coming over as arrays. Particularly in the case of dealing with entity reference fields, or other fields that have multiple values. An example migration may be like the following:

  source:
    plugin: d7_node
  process:
    temp_body:
      plugin: iterator
      source: field_section
      process:
        target_id:
          plugin: migration
          migration: field_collection_field_section_to_paragraph
          source: value
    temp_images:
      plugin: iterator
      source: field_image
      process
        target_id:
          plugin: migration
          migration: image_entities_to_paragraph
          source: fid
    paragraphs_field:
      plugin: merge
      source:
        - @temp_body
        - @temp_images
  destination:
    plugin: 'entity:node'

In this case I'm able to merge entities created from 2 different migrations into one entity reference field (specifically paragraphs). You might even be able to do this with 2 different types of paragraphs created using the EntityGenerate Process Plugin.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

generalredneck created an issue. See original summary.

generalredneck’s picture

Status: Active » Needs review
FileSize
1000 bytes

Lets try this on for size

generalredneck’s picture

FileSize
2.15 KB

We actually need merge not Union like my first attempt was

FluxSauce’s picture

Status: Needs review » Needs work

Seems to function, but includes two var_dumps.

drush mi NAMEOFMIGRATION
array(2) {
  [0]=>
  array(3) {
    [0]=>
    array(2) {
      ["target_id"]=>
      string(3) "127"
      ["target_revision_id"]=>
      string(3) "127"
    }
    [1]=>
    array(2) {
      ["target_id"]=>
      string(3) "128"
      ["target_revision_id"]=>
      string(3) "128"
    }
generalredneck’s picture

Status: Needs work » Needs review
FileSize
2.1 KB
718 bytes

FAILURE!

Ok, so here's the debug free version.

mikeryan’s picture

Status: Needs review » Fixed

Committed, thanks!

Jaesin’s picture

The Merge plugin throws MigrateException when source data isn't as nice as it could be.

In general, migrate handles source input pretty well as far as dealing with a single value vs an array.

<?php
      if (!is_array($item)) {
        throw new MigrateException('One of the items is not an array that can be merged.');
      }
      $new_value = array_merge($new_value, $item);
?>

Could be replaced with:

<?php
    foreach($value as $item) {
      $new_value = array_merge($new_value, (array) $item);
    }
?>

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.