The migrate field handler throws away multiple references during import. See code comment below.

Example Migration:

  // ... some migration code her
  $this->addFieldMapping('field_rel_lineart', 'PDM_lineArt_new')
     ->sourceMigration('AveryMediaItemMigration')
     ->separator(',')
   // ... more field mappings, etc.

  //Prepare Row some lines deeper.
  public function prepareRow($row) {
   
    $row->PDM_lineArt_new = '123,456,789';

  }

Migrate Handler Code:

  /**
   * Field-level prepare.
   *
   * Converts incoming data into the proper field arrays for entityreference
   * fields.
   *
   * @param object $entity
   *   The destination entity which will hold the field arrays.
   * @param array $field_info
   *   Metadata for the entityreference field being populated.
   * @param array $instance
   *   Metadata for this instance of the entityreference field being populated.
   * @param array $values
   *   Array of values for the entityreference field.
   *
   * @return array|null
   *   An array of entityreference fields.
   */
  public function prepare($entity, array $field_info, array $instance, array $values) {
    $arguments = (isset($values['arguments'])) ? $values['arguments'] : array();
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
    // Setup the standard Field API array for saving.
    $delta = 0;

    //@QUESTION: We always get an array, don't we? Why putting the array into another array with always one value?
    if (!is_array(reset($values))) {
      $values = array($values);
    }

    //@FIXME: This foreach loop runs always one time and therefore all other values are thrown away.
    foreach (array_filter($values) as $value) {
      $return[$language][$delta]['target_id'] = reset($value);
      $delta++;
    }
    // Return NULL for no values in the field.
    return isset($return) ? $return : NULL;
  }
}

Sending a patch to fix this later.

Only local images are allowed.

Only local images are allowed.

Only local images are allowed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcusx’s picture

Status: Active » Needs review
FileSize
850 bytes

Here is a patch that removes the wrapping of array into other array and therefore the need for getting the value through reset(). There might be good reasons to do all that, but I don't see them.

Maybe someone can explain.

jbiechele’s picture

Above fix outputs in my case a wrong format of the array containing the migrated entity reference values aka "target_id".

Sample of a working format would be:

$return => Array 
(
    [und] => Array
        (
            [0] => Array
                (
                    [target_id] => 147740
                )

            [1] => Array
                (
                    [target_id] => 148333
                )

            [2] => Array
                (
                    [target_id] => 148330
                )

            [3] => Array
                (
                    [target_id] => 148346
                )
        )
)

The above patch instead delivers the following in my case

$return => Array  
(
    [und] => Array
        (
            [0] => Array
                (
                    [target_id] => Array
                        (
                            [0] => 147740
                            [1] => 148333
                            [2] => 148330
                            [3] => 148346
                        )
                )
        )
)

which results in broken references.

marcusx’s picture

Issue summary: View changes
FileSize
29.15 KB

@jbiechele

What was your solution for the broken array in your case. I just used the patch again on another migration and get exact the result you would expect.

Krumo Screenshot

marcusx’s picture

Issue summary: View changes

Removing accidentally changes to the issue summary.

gionnibgud’s picture

It works for me!

das-peter’s picture

jbiechele’s picture

Issue summary: View changes

@marcusx
Sorry for the long time in answering. Unfortunately I cannot reproduce the case reported in #2, I apologize.
I can confirm that your patch from #1 works now in a new migration I'm working on.

marcusx’s picture

@das-peter

So is #2394725: Migrate multiple entity reference fields using migrate module support fixing this issue and I can close it and remove the patch after updating to the latest version of "Entity Reference"?

das-peter’s picture

@marcusx I can't answer that. I had an issue which is perfectly described by both issue titles. The one I linked solved my issue. I didn't check the patch here, so I guess best bet is to update migrate to latest dev, test it and if it's working for you too close this issue. If it isn't working another round of debugging and patching.

amitaibu’s picture

@marcusx can you please indeed check if the -dev version fixes your issue.

gettysburger’s picture

Where does this stand? I need to migrate multi-value taxonomy terms and am looking for a fix. Thanks.