Your Guide To Extending RSS Feeds in Drupal

If you need to provide updated information from your site to external consumers, RSS is generally the go-to standard you use. It provides a well-defined and well-supported definition. Generator and consumer RSS solutions are widely available in pretty much any programming language or web framework out there. Drupal is no exception with support built directly into Drupal core, and Views has its own display plugin for more selective RSS feed generation. However, what do you do if you need finer-grained control over your feed generation, and possibly even custom extensions that comply with the RSS standard?

Views RSS

One great solution is the Views RSS module. Out of the box it adds customizable support for a core RSS channel, item level elements, and adds Dublin Core support as well. By itself, it provides a much more flexible solution built on top of field-based Views than the one that comes with Views. In order to use it, download and install the views_rss module and the views_rss_core (and optionally views_rss_dc) module. Now when you create a View RSS Feed, you have a new format type to pick called RSS Feed – Fields:

A view off RSS screenshot

This enables the Views RSS display format plugin, where you can begin to customize all aspects of a standard RSS feed (note the views_rss_dc module was enabled on this site):

screenshot of views style options

 

For those not familiar with RSS internals, the channel elements occur at the top level of the RSS feed, and typically consist of identifying information for the feed, update times, descriptions, etc.

screenshot of description input in Views Feed style options

Item elements are attached to each individual item that is returned in an RSS feed; Views items are analogous to rows and in which you map result fields.

screenshot of Views Feed style options

Other feed settings provide options that govern how Views RSS generates the feed.

Create your View by adding the fields you want to make available to the RSS feed and then go into the Views RSS display settings above and map fields to item elements to customize your feed output. Save the View, and when you browse to the path that you defined for the RSS feed, you should see your customizations come across in the RSS data.

Going One Step Further…

OK, great – now you have mapped all your fields into an RSS feed, but the consumer for the feed announces that they want to also have additional information that is not provided by the RSS core elements (channel or item).

What now?

Well, it just so happens that Views RSS provides a plugin mechanism for creating custom namespaces and elements for both the channel and item levels. Currently there are several contributed plugins that extend Views RSS with the most common elements, which are listed on the Views RSS project page. If you can’t find what you’re looking for there, then building your own is pretty simple. Documentation is available on the Views RSS project page, but the short and sweet version is simply this: the module exposes everything needed to do so via the Drupal hook system (list pulled from the Views RSS documentation):

Hooks for defining new namespaces, <channel> and <item> elements, and date sources:

  • hook_views_rss_namespaces()
  • hook_views_rss_channel_elements()
  • hook_views_rss_item_elements()
  • hook_views_rss_date_sources()

Hooks for altering definitions provided by other modules:

  • hook_views_rss_namespaces_alter(&$namespaces)
  • hook_views_rss_channel_elements_alter(&$elements)
  • hook_views_rss_item_elements_alter(&$elements)
  • hook_views_rss_date_sources_alter(&$date_sources)

Hooks for processing admin configuration form:

  • hook_views_rss_options_form_validate($form, &$form_state);
  • hook_views_rss_options_form_submit($form, &$form_state);

Implementing these hooks is very straightforward. I’ll provide examples for a basic hypothetical module, views_rss_example, that adds a namespace, single channel element, and single item element.

First, you add the new namespace to the Views RSS framework:

Next, you add channel elements (if your namespace calls for them):

Then, you can add item elements (again if your namespace calls for them):

Presto! You now have a new namespace added to your RSS output, a channel element that you can provide the value for, and an item element that can have a Views field mapped to it.

The use of hook_views_rss_date_sources() is limited to supporting non-core Views data sources, so if you’re querying standard node, user, comment, and file tables in your view, you don’t need to mess with it. Similarly, unless you are trying to extend the Views RSS configuration beyond simply adding elements and namespaces, there is rarely a need to implement the  hook_views_rss_options_form_X () hooks. The alter hooks are standard fare – they are invoked after all construction hooks have been called and allow a module to alter the provided items.

Conclusion

The Views RSS contrib module provides great scaffolding for quickly extending the standard Views feed mechanism. Several extension modules already exist for common extensions for various vendors or services, so often you don’t need to roll out your own. But if you do, the framework is there to do so.

To see more detailed examples, including alternative data mapping methods and preprocessing of element values, check out the Views RSS documentation and poke through the code that ships with Views RSS, specifically views_rss_core and views_rss_dc.

  • Drupal Kurdish

    Hi, Robert thank you , for this Guide , however , I am looking to fetch content from other Websites are not provide RSS , or XML , at all. thank you .

    • arpieb

      Well, that’s going to be a totally different mechanism – it sounds like you’re looking for some kind of HTML parsing solution. If you’re using PHP, I’d recommend using something like QueryPath (http://querypath.org/) to parse content from an HTML page.

      However, if the site you’re trying to pull content from doesn’t provide an RSS or XML feed – they most likely don’t intend for their content to be syndicated. You might want to take that into consideration as copyright issues might arise.

      • Drupal Kurdish

        thank you, however the Language I am trying to Work on there is not much Regulation Copyrights, applied yet . anyway thank you again , for replying I AM appreciate it.

  • bangpound

    Views RSS is one method, and the modules you described are fine ones to use. I needed to make a podcast feed, and iTunes Views RSS module (which has dependencies on still other Views RSS modules) was not up to the task. Supporting additional fields was difficult especially when working with files or images.

    The Views UI cannot hold the couple of dozen fields required to configure a iTunes podcast. It tried, but it’s a lot of fields all configured in one difficult to navigate modal.

    The solution I implemented is to use entity view modes and RSS Field Formatters https://drupal.org/project/rss_field_formatters. While I still had to write some code, working with entities and field formatters was a lot easier than Views RSS. There are fewer dependencies, too.

  • thetraveler

    I’m working on a Drupal 6 site for a client and I already have a RSS feed set up with views from one of the content types. The RSS feed is mostly working, but it doesn’t work with all the feed readers. Error: Missing atom:link with rel=”self”
    So, I installed the Views RSS module, but I can’t seem to get the “How should this field be styled” or add the fields to my RSS views in Drupal 6. Any advice on how to configure or set up this module in D6, so I can add an Atom link to my RSS feed?

  • Pingback: [Drupal] Niedzielna lekturka w temacie Drupala #3 » MatiWeb.com