Your Guide To Extending RSS Feeds in Drupal

Your Guide To Extending RSS Feeds in Drupal

Robert Bates, Senior Developer
#API | Posted

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:

  1. <?php
  2. /**
  3.  * Implements hook_views_rss_namespaces().
  4.  */
  5. function views_rss_example_views_rss_namespaces() {
  6. $namespaces['example'] = array(
  7. 'prefix' => 'xmlns',
  8. 'uri' => '',
  9. );
  11. return $namespaces;
  12. }
  13. ?>

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

  1. <?php
  2. /**
  3.  * Implements hook_views_rss_channel_elements().
  4.  */
  5. function views_rss_example_views_rss_channel_elements() {
  6. $elements['example:publisher_id'] = array(
  7. 'title' => t(‘Publisher ID for this vendor’),
  8. 'description' => t(‘Provides the publisher ID for the vendor for some reason’),
  9. );
  10. return $elements;
  11. }
  12. ?>

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

  1. <?php
  2. /**
  3.  * Implements hook_views_rss_item_elements().
  4.  */
  5. function views_rss_example_views_rss_item_elements() {
  6. $elements['example:geotag'] = array(
  7. 'title' => t(‘Geolocation for this item’),
  8. 'description' => t(‘Provides the lat/lon GPS coords to pin this item to’),
  9. );
  10. return $elements;
  11. }
  12. ?>

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.


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.

Robert Bates

Senior Developer