I'm experiencing some strange behavior when using multiple Entity Reference Feeds mapping targets in a CSV importer. I have one column called "Asset IDs" and one called "Asset names" to allow users to reference entities using their IDs and/or their labels.

The expected result is that Entity Reference's Feeds code would intelligently merge the content of the two columns in the finished entity. However, this is not how it works now - and it's possible to end up with strange results.

In practice, it probably makes sense to discourage using two separate columns mapping to the same field like this, but I would still consider this a bug because it is technically possible.

The issue is in the entityreference_feeds_set_target() function, which can be viewed here: https://git.drupalcode.org/project/entityreference/blob/7.x-1.5/entityre...

The function has an implicit assumption that only one column in the importer will be mapped to the target reference field. It creates an $iterator variable (at line 107 in the link above), which it increments as it iterates over the values from the column it is currently processing. Then it assigns the entity value using that variable:

// Assign the target ID.
$field[$language][$iterator]['target_id']   = $entity_id;

This means that the first column in a CSV will be processed, and values will be assigned to the entity. But then a second column will overwrite them one-by-one (because $iterator is reset to 0).

Solving this will require refactoring this code so that it looks for existing values on the entity before assigning new ones. This would also require checking to see if a value already exists in the entity - and only adding it if it doesn't exist - UNLESS the Entity Reference's field setting "Allow the same entity to be referenced multiple times" is TRUE.

Comments

m.stenta created an issue.