Home >> Archive >> 2015 >> Feeds snippet to import full csv row to a field
Tuesday 7 April 2015
Tags: 
Feeds
Snippets

Recently we were using Feeds 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 for more information.

Article Archive

Try another theme