Example FeedAPI Processor

Events happening in the community are now at Drupal community events on www.drupal.org.
You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!

FeedAPI Node lets you aggregate feed items into nodes. What if you need slightly different behavior but still want to retain the most of the functionality of FeedAPI Node (save feeds as nodes, check whether nodes already exist before importing them, deleting nodes when content expires, etc)? Just create an additional processor, weight it "lighter" than the FeedAPI Node processor and enable it. Note: the code below will only work if another processor is enabled because it does not implement its own saving or purging of feed items. Also, it must be given a lower weight so that it executes before the FeedAPI Node processor so that it can modify each feed item before FeedAPI Node processor saves it.

This example addresses the problem of parsing the date of an aggregated item directly from its URL. This should work for items that encode the date in the URL in the following format: YYYY/MM/DD

What You'll Need

  • FeedAPI
  • FeedAPI Node
  • SimplePie or Common Syndication Parser

Example

/**
* Implementation of hook_feedapi_item(). Timestamp of each feed_item
* will be parsed from the URL instead of being taken from the timestamp field.
*/
function feedapi_item($op) {
  switch ($op) {
    case 'type':
      // Return
      return array("XML feed");
    case 'save':
      // Modify the feed_item to contain the timestamp parsed from the URL instead.
      // $feed_item comes from where, exactly?  The second, undocumented argument
      // to this hook is $feed, so presumably can get it from inside there, someplace.
      $item_url = $feed_item->options->original_url;
      preg_match('/[0-9]{4}\/[0-9]{2}\/[0-9]{2}/', $item_url, $matches);
      $feed_item->options->timestamp = strtotime($matches[0]);
      return $feed_item;
    case 'update':
      break;
    case 'delete':
      break;
    case 'load':
      break;
    case 'unique':
      // Simplest is to make this return true so that we only have to implement the 'save' operation
      // and not the 'update' operation.
      return true;
    case 'fetch':
      break;
    case 'expire':
      break;
    case 'purge':
      break;
    default:
      if (function_exists('_feedapi_node_'. $op)) {
        $args = array_slice(func_get_args(), 1);
        return call_user_func_array('_feedapi_node_'. $op, $args);
      }
  }
}

RSS & Aggregation

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week