Feeds: importing multiple values into one multi-value field

Feeds: importing multiple values into one multi-value field

This post is a short how-to on importing data from multiple fields of a comma separated value (CSV) file into one multi-value field using the Drupal Feeds Tamper.

Chances are you have been in a situation where you need to import some data into a Drupal site. The data was provided to you in a CSV file. Because you are smart and know better than to spend your client’s money writing your own code for this, you grab the Feeds module which conveniently includes a CSV parser.

All is well until you need to merge the content of multiple sources (eg: “cells”) into one target (eg: “field”). This might be the case if you have a List field where multiple options can be checked.

screenshot of the content of our example CSV file

For each of these names we want all of the certifications to be passed to a single field:

View of our list field widget

This is where Feeds Tamper shines. Here’s how you do it:

1. Go to your feed processor mapping page and map all three sources to the one target:

View of our Source / Target mapping

2. Go to the Tamper tab (if you are using the dev version) or to the Feeds Tamper link above the mapping table (if you are using the beta-3 version) and add a plugin for the last of your 3 sources (in our example certification_three).

3. Select the Rewrite plugin and use the tokens to rewrite the input to include all of your sources separated by a comma and save.

A view of the configuration of our Rewrite plugin.

4. Add a second plugin: Explode. Make sure that the string separator is the same as the one you have used in the Rewrite plugin. Save.

5. Run your import et voilà!

A couple of gotchas: there seems to be some cases where this module misbehaves. In the example I just described, each of the certifications is numbered by a string. Using an integer (eg: certification1, certification2) made the Rewrite plugin fail. Indeed instead of the value the token represents, you will get the label of the token itself.

Additionally gravit mentions on d.o:

It seems feeds tamper goes in alphabetical order for some things - If you are using Feeds Tamper to do any sort of field alteration (find and replace, convert to boolean, etc) You need to make sure the "last mapper" is also the last field alphabetically. Otherwise your field alterations will not happen before the "rewrite" filter on the "last mapper".

 

Thanks to danon for his comment that inspired this post.