Mapping multiple values in one field

Last updated on
11 March 2021

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

This documentation needs work. See "Help improve this page" in the sidebar.

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.

Help improve this page

Page status: Needs work

You can: