Skip to main content

Feeds snippet to import full csv row to a field

Published on 2015-04-07

Recently we were using Feeds(link is external) to import CSV files to a Drupal content-type, but we also wanted to preserve the original CSV row in a field.

By default you can map and import values of a known column (with their corresponding header) by typing the header as 'Source' and choose your field as 'Target', but you cannot map 'all columns' / 'full row' to a field.

This snippet adds this fullrow pseudo column to your CSV import that you can map on a field.
The column is added after the CSV parser is finished (see hook name), and just before the Processor takes over the task and starts saving nodes.

In this example the Source name is fullrow. All columns (also 'unmapped' ones) are glued together with a space as delimiter.

The module name in this example is MYMODULE.

/**
 * Implements hook_feeds_after_parse().
 */
function MYMODULE_feeds_after_parse($source, $result) {
  if ('FeedsCSVParser' != get_class($source->importer->parser)) {
    return;
  }

  // Add 'fullrow' column with space delimiter.
  foreach ($result->items as &$item) {
    $item['fullrow'] = implode(" ", array_values($item));
  }
}

See Feeds API(link is external) for more information.