VerRevisiones

Adding a comment RSS feed to Drupal nodes

Yesterday I was able to configure my Drupal installation to provide each node with an RSS feed for its comments. Though it wasn't a particularly difficult task, it required some testing and reading of docs, but the procedure is quite easy to reproduce, thanks to the effort that Drupal developers use to place in the development of their code. These are the necessary steps:

  1. Download the views module and unpack it into your modules directory. Enable all Views modules under Administer / Site building / Modules and save the new configuration.
  2. Now we need to create a new view. Fortunately, views can be exported and imported, so all you need to do is go to Administer / Site building / Views / Import and paste the content of the file attached below in the text box. After that, you should be able to access http://example.com/drupal/node/1/feed (tailor to your own URL schema) and see an RSS feed of comments in node 1. Furthermore, a combined feed of comments in all nodes should become available at http://example.com/drupal/node/all/feed.
  3. To make the feed discoverable, you'll have to modify your theme's node.tpl.php file. Just add the following code snippet near the beginning:

    <?php
    if ($page) {
    drupal_add_feed($node_url . '/feed', t('Comment RSS - ') . $title);
    }
    ?>

    The code adds the link to the RSS feed in the HTML head of nodes, but only if they're being displayed in a page by themselves. The text Comment RSS - can be translated as any ordinary Drupal interface string.

And that's all there is to it. I suggest that you edit the view using Administer / Site building / Views to see how it was created (I got the inspiration from this example). Also, it's good to install the advanced help module to access Views' included docs. Views is a complex but very powerful and well-designed module, and it's definitely worth mastering it a little.

Update (2009-11-16): The original code made the feed's main link point to its own URL, which was pointless. I haven't been able to make the link point to the node's URL; instead, the feed now points to http://example.com/drupal/node/1/view, which is a view of the same feed as a page. This display is also rather pointless, but it allows us to catch its path in the server and redirect it to the node's URL like this (Nginx web server):

rewrite ^/drupal/(.+)/view$ /drupal/$1 redirect;

or like this (Apache web server, untested):

RedirectMatch /drupal/(.+)/view /drupal/$1

I admit this is a semi-ugly hack. If anyone has a better idea, please tell me.