Feeds Tamper plugin for getting the feed language

Posted on 13/04/2016

Feeds module gives you a very flexible and open-ended way of importing content from various sources. It belongs in the group of big and abstract modules, among with Rules and Views for example, that allow you to achieve very much, without any coding.

The most useful additional module built on top of Feeds is probably the Feeds Tamper module. It allows you to alter the data before it is stored in your database. This is necessary as the data sources you will be using will not always have standardized properties or formats, and you will need to work with the data, before Drupal can use it.

By default Feeds Tamper comes with 32 different plugins (or "converters"), that allow you to turn time strings into UNIX timestamps, encode/decode HTML entities, and so on. However, one of the limitations of both the Feeds and Feeds Tamper modules, is that you are able to reference only certain values of the parent node, such as the language.

You would need this if you are importing content from RSS feeds on a multilingual website for example. To achieve this, you will need to declare a plugin in your custom module, and fetch the language of the parent node.

The plugins functionality in Feeds Tamper module is provided by CTools. This means that first we will have to implement the hook_ctools_plugin_directory(), and precise where will our plugin files be located:

  1. /**
  2.  * Implements hook_ctools_plugin_directory().
  3.  */
  4. function YOUR_MODULE_ctools_plugin_directory($owner, $plugin_type) {
  5.   // Provide our directory for storing the feeds tampering plugin.
  6.   if ($owner == 'feeds_tamper' && $plugin_type == 'plugins') {
  7.     return 'plugins/feeds_tamper';
  8.   }
  9. }

For more information on CTools plugins, see the ctools_plugin_example.module.

The next and last step is to create the language.inc file in plugins/feeds_tamper directory of your module:

  1. $plugin = array(
  2.   'form' => 'YOUR_MODULE_tamper_language_form',
  3.   'callback' => 'YOUR_MODULE_tamper_language_callback',
  4.   'name' => 'Set the feed node language',
  5.   'multi' => 'loop',
  6.   'category' => 'Other',
  7. );
  8.  
  9. function YOUR_MODULE_tamper_language_form($importer, $element_key, $settings) {
  10.   $form = array();
  11.   $form['html'] = array(
  12.     '#markup' => t('This will set the imported node language to the same language as the parent feed node.'),
  13.   );
  14.  
  15.   return $form;
  16. }
  17.  
  18. function YOUR_MODULE_tamper_language_callback($result, $item_key, $element_key, &$field, $settings, $source) {
  19.   // Sanity check. Skip processing if the importer is not attached to a feed
  20.   // node.
  21.   if (isset($source->feed_nid)) {
  22.     // Load the source node.
  23.     $source_node = node_load($source->feed_nid);
  24.     // Set the field value to the same language on the source feed.
  25.     $field = $source_node->language;
  26.   }
  27. }

And that's it! To use this, you can simply map the "Blank source" to target node language, and then on tampering subpage choose "Set the feed node language" plugin.