Feeds API Examples on Drupal 7 (developer guide)

  • Posted on: 21 September 2016
  • By: jmu

For the production web, importing large data is very important. When using Drupal, Feeds Module is the best choice for importing data. They released a very stable version for 7.x, but version 8 is not ready yet. If you are using Drupal 8, you can contact them to tell them your usage and help them improve their work.

The description of this module is pretty clear on the download page:

Import or aggregate data as nodes, users, taxonomy terms or simple database records.

  • One-off imports and periodic aggregation of content
  • Import or aggregate RSS/Atom feeds
  • Import or aggregate CSV files
  • Import or aggregate OPML files
  • PubSubHubbub support
  • Create nodes, users, taxonomy terms or simple database records from import
  • Extensible to import any other kind of content
  • Granular mapping of input elements to Drupal content elements
  • Exportable configurations
  • Batched import for large files

Base on what you need, you can set new imports on your website. There are three fetchers: Database Fetcher, File upload, HTTP fetcher. And each fetcher will have some different parsers. In this blog, we will use "CSV Parser" under "File Upload Fetcher" becasue importing csv file is a very common way. 

Hint: Go to Structure/"Feeds importers" for setting new imports. (I provide a sample feeds importer setting below):feeds importer sample

While you creating the new imports, you will see a lot of settings. Those are all basic setting that you can use through Feeds GUI. But if you want to make some change on these basic functions or add any new function, you will need to use Feeds API to do that. Create a new custom module and then use those hooks to implement your needs. I will give some examples for different hooks below:

1. Processing Data Before Feed Item is Saved

    This hook is used for pre-process those feed items before saved. 

    Below is a function that import products which have productName (the machine name for import is "product_import". the column name we will check in csv file is "productName"):

function hook_feeds_presave(FeedsSource $source, $entity, $item) {
	// check that this is fired only for the importer being used
	if($source->importer->id=='product_import'){
		if(!strcmp($item['productName'],"")){
			$entity->feeds_item->skip = TRUE;
    	      }
         }
  }

 

2. Be called after a feed source has been parsed, before it will be processed

    This hook is very useful. Especially, we are trying to modify the data from the source. My personal example is based on using the CSV Parser. Here is my sample description:

    I use the commerce drupal module and have already import some products with title and sku correctly. Right now, I would like to create product display(content type) for each product by using import. During the feed import setting, I set two columns under "Mapping for Node processor" -- (Source->Target) Title->Title ; Sku->Product:SKU. But in my csv file, I only have one column named "SKU" and will read the title from the database base on the sku I provided. 

function hook_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  if($source->importer->id=='product_display_import') {

		$count=0;
		foreach ($result->items as $items) {
			$count++;
		}

		for($i=0;$i<$count;$i++){
			/******* query to find title and put it in the source *******/
			$query= db_select('commerce_product','cp');
			$product_query = $query->fields('cp',array('product_id','title'))
				->condition('cp.sku',trim($result->items[$i]['sku']),'=')
				->execute()
				->fetchAll();
			$title = $product_query[0]->title;
			$result->items[$i]['title'] = $title;
			
		}
	}
}

feeds importer pre process

Reference:

  1. Mike Stiv Blog: He is one of the committers for Feeds Module.
  2. Programmatically execute a Feeds importer in Drupal 7: This blog provides some Feeds API using.
  3. How to check the value from data sheet during Feeds import?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.