[Drupal] How to create a new parser in Feed API module?

| | 2 min read

The feedAPI module is a famous one for processing custom feeds in different formats like rss/Atom, csv, iCal etc. Now the feedAPI has been upgraded to the "feeds",which have more features and easy to use and customize. However still a lot of Drupal 6 sites uses the feedAPI module for handling external feeds. Let us see how a new parser can be created in feed API module.

The module provides a lot of third party parsers to parse each kind of feed types, some are,

  • The RSS/Atom feeds can be parsed using the Common Syndication Parser (part of FeedAPI).
  • The iCal feeds can be parsed with the iCal parser.
  • The CSV files can be aggregated with CSV parser.
  • The KML feeds can be parsed with KML parser

But the feedAPI module implemented each of these parsers as separate modules, so if you need to create a new parser you need create a new module for it. There will be multiple situations when you need to implement a new parser, like when you need to process some custom feed content with custom tags, or need to implement a parser for some new type of feeds for which a parser is not available. Anyway I will explain some basics for creating a new one.

  • Creating a help case

    This is first step, feels like some weird way to define a new parser, however you need to implement the path 'feedapi/full_name' and need to return the name of the new feed parser here. Which means a single module can only implement a single parser.

  • Implement the hook_feedapi_feed

    Now you need to implement the hook "hook_feedapi_feed" to implement the basic working of your feed parser. Here you have to define the 3 values of the operations($op) parameter, they are,

    • type

      Here you can define the types of feeds your parser is going to deal with.

    • parse

      Here you can actually parse the feed object you received, you can depend on the PHP's simplexml functions to convert the xml file into a feed object. All the operations related to parsing the fed object can be done here.

    • compatible

      In this operation you can check whether the incoming feed is compatible for your parser or not.

  • Implement hook_feedapi_settings_form

    If the parser provides any settings that are specific for the feed it handles, you can create a settings form by implementing this hook. If this hook is implemented then at the time of parser selection for a particular node the settings form is displayed.

    If no settings form is created for a particular parser then the selected parser will not be showed upon the node edit page.