Skip to main content
RSS Feed Importers

Programmatically create and trigger feeds importer

We met with a challenging requirement where we needed to create a feeds importer on node creation of particular content type. It’s like for ‘n’ number of nodes there must be an ‘n’ number of feeds importer.

For that I created a feeds importer which will be considered as the template. Whenever the node of specific content type is created, the template will be cloned and a new importer will be created.

The following code needs to reside in hook_node_insert() and will be used to clone the feeds importer:

// Pass the feed_importer_id which is already created
$template = feeds_importer('template');
// Get the exported code of already created feeds importer
$current_config = $template->config;
// Changed the name and description in configuration
$current_config['name'] = $node->title;
$current_config['description'] = $node->title;

// Start to create the new importer, argument will be a importer id for the new importer.
$new_importer = feeds_importer('ImporterId');
// Pass the configuration to the new importer.
$new_importer->setConfig($current_config);
foreach (array('fetcher', 'parser', 'processor') as $type) {
  $save->setPlugin($current_config->config[$type]['plugin_key']);
  $save->$type->setConfig($current_config->config[$type]['config']);
}
// Save the importer.
$new_importer->save();


The above code will create a new feed importer with id ‘ImporterId’ on node creation.

Comments

Hi, what I was done is, first I was created a feed importer in UI which is considered as common feed importer. Whenever I created a node I cloned that common importer and created a new importer by using the above code. Simply what that code does means, It will clone the existing feed importer and created a new feed importer.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.