Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#3 | migrate_multival_entitref.jpg | 29.15 KB | marcusx |
#1 | entityreference-fix_multivalue_import-2434333-1-D7.patch | 850 bytes | marcusx |
Comments
Comment #1
marcusx CreditAttribution: marcusx commentedHere 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.
Comment #2
jbiechele CreditAttribution: jbiechele commentedAbove 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:
The above patch instead delivers the following in my case
which results in broken references.
Comment #3
marcusx CreditAttribution: marcusx commented@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.
Comment #4
marcusx CreditAttribution: marcusx commentedRemoving accidentally changes to the issue summary.
Comment #5
gionnibgud CreditAttribution: gionnibgud commentedIt works for me!
Comment #6
das-peter CreditAttribution: das-peter at Cando commentedAdded related issue.
Comment #7
jbiechele CreditAttribution: jbiechele commented@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.
Comment #8
marcusx CreditAttribution: marcusx commented@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"?
Comment #9
das-peter CreditAttribution: das-peter at Cando commented@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.
Comment #10
amitaibu@marcusx can you please indeed check if the -dev version fixes your issue.
Comment #11
gettysburger CreditAttribution: gettysburger commentedWhere does this stand? I need to migrate multi-value taxonomy terms and am looking for a fix. Thanks.