Community Documentation

Mapping multiple values in one field

Last updated August 8, 2012. Created by juampy on December 4, 2011.
Edited by sprice. Log in to edit this page.

While setting up a feed, you may need to map, merge or process more than one field into a single field, no matter if the input is coming from columns from a CSV, tags from an XML or any other source.

Example

Suppose that our source is a very simple CSV where there is the following structure:

Name Surname
Iggy Pop
Tom Waits

We want to merge both Name and Surname into a single field for our users table. Therefore, we will join this together into a new column "fullname" at hook_feeds_after_parse() and then define a mapper for this.

Here is the hook implementation:

/**
* Implements hook_feeds_after_parse()
*
* Adds a key "fullname" to each result
* @param $source
*  FeedsSource object that describes the source that has been imported.
* @param $result
*   FeedsParserResult object that has been parsed from the source.
*/
function mymodule_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  foreach($result->items as $key => $row) {
    $result->items[$key]['fullname'] = $row['name'] . ' ' . $row['surname'];
  }
}

Then, when you are configuring the feed in the mappings page, you could now use the source 'fullname' and map it to the field Name of a user.

Comments

This can be done with feeds

This can be done with feeds tamper. By assigning both CSV fields to Temporary Values then calculate the final value inside the tamper and assign to your node.

- bbros.us -
Who else is interested in Lunch?

Help improving the docs

Great, could you edit the page and add it as an alternative method with a step list?

Page status

Needs technical review

Log in to edit this page

About this page

Drupal version
Drupal 6.x, Drupal 7.x
Audience
Programmers
Level
Intermediate, Advanced
Keywords
feeds

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here