How to display an RSS feed in a Drupal block

How to set up Drupal Feeds to Import RSS

How to display an RSS feed in a Drupal block

If you have a standard RSS feed that you'd like to display in a block on your Drupal website, you've come to the right place. For this example, we will be using a sample feed at http://www.feedforall.com/sample.xml (excerpted here:). 

<item>
    <title>RSS Solutions for Restaurants</title>
    <description>
        <b>FeedForAll </b>helps Restaurant's communicate with customers. Let your customers know the latest specials or events.<br> <br> RSS feed uses include:<br> <i><font color="#FF0000">Daily Specials <br> Entertainment <br> Calendar of Events </i></font>
    </description>
    <link>http://www.feedforall.com/restaurant.htm</link>
    <category domain="www.dmoz.com">
        Computers/Software/Internet/Site Management/Content Management
    </category>
    <comments>http://www.feedforall.com/forum</comments>
    <pubDate>Tue, 19 Oct 2004 11:09:11 -0400</pubDate>
</item>

Let's say we want to display the two of the titles linked to the appropriate URL, along with their description, in a block like this: 

drupal feeds block with views

General Setup

We'll need the following modules:

And here is our general approach. We'll dive into the details next.

  • We will create 2 content types:
    • RSS Feed - we'll use this to tell Feeds about the XML feed we're importing
    • RSS Item - this will be the nodes for displaying the content from the feed
  • We will also create a Feeds Importer - this will tell Feeds what type of data we're importing, where we're importing it to, and how this RSS feed relates to our custom content types. 
  • We will set this up so the site checks for updates to the RSS feeds periodically using cron.
  • Last we'll create a block using views that display the most recent items in this feed. 

Feed Content Types

Why do we need 2 content types you might be thinking? Our RSS Feed content type will be very simple, we will just use it to save the URL of our feed, in this case, http://www.feedforall.com/sample.xml. The RSS Item content type will be used to save the individual items in the feed. In this case, it would contain the title, description, and link from the RSS feed. So go to admin/structure/types/add and create your new content type for your RSS feed. To keep things simple, I turned off authoring information, don't promote to front page, and turned off comments. 

xml feed content type

Next create your RSS Item content type

xml item content type

This one required the addition of fields. These will match the items in the XML feed that you want to pull into each feed item. I just changed the Body field to Description, and added a Link field.

rss item fields

This matches the information in the RSS feed.

<description>
        <b>FeedForAll </b>helps Restaurant's communicate with customers. Let your customers know the latest specials or events.<br> <br> RSS feed uses include:<br> <i><font color="#FF0000">Daily Specials <br> Entertainment <br> Calendar of Events </i></font>
    </description>
    <link>http://www.feedforall.com/restaurant.htm</link>

Set up Feeds module to import

If you haven't already, download the Feeds module and enable Feeds and Feeds Admin UI. 

drupal feeds module

We'll now configure Feeds to import at admin/structure/feeds by clicking on Add Importer, giving our importer a name, and clicking Create.

set up drupal feeds importer

After creating our importer, we'll be presented with a bunch of configuration options. First, under Basic Settings, we will attach our importer to the RSS Feed content type. I've unchecked 'Import on Submission' so we can confirm that this will work when we run cron (select As often as possible for Periodic Import so it will import as soon as you run cron). If you want your Feeds to be imported as soon as you save a new RSS Feed node, you can leave that checked. Save.

drupal feed basic settings

Under Fetcher, we'll select HTTP Fetcher, and you shouldn't need to do any configuration for the HTTP Fetcher Settings. 

Under Parser, if this is a standard RSS feed, you can select Common Syndication Parser, and there won't be any settings for this.

Lastly under Processor, select Node processor, as we will be importing the feed items into our Feed Items content type. Here we will need to do some quick configuration for the Node processor settings. These settings are a little up to you in terms of if you want to update existing nodes, text formats, and the author that will be used as the author of the nodes. The main thing is for the Bundle, you want to select the RSS Item content type. This is telling feeds what nodes to create when importing feed items. For our example, we'll select the RSS Item content type. If there is any HTML in your feed item content (like in the description) select Full HTML as the input format. Save.

 drupal feeds node processor settings

Then click on Mapping under the Node Processor - here is where we'll tell Feeds which fields to populate on our Feed Item content type with the information in the feed. The GUID is the unique identifier for each feed item, and we need to have at least one unique ID. 

Run your first import

Now, if you go to create a new RSS Feed node (node/add/rss-feed) you'll see the Feeds module has add a Feed URL required field. Enter in the URL of the RSS feed and hit Publish. If under your Basis Settings for Feeds you had checked the 'Import on Submission' option, when you saved this RSS Feed node, Feeds would have imported the new RSS Items for you, but since we left this unchecked, saving this node won't do anything except create a new node. 

drupal feeds import feed

Now, when we run cron, we'll see that Feeds will import new nodes.

drupal feeds successful

If we go to the content overview page, we'll see that 9 new RSS Item nodes have been created. 

drupal feeds new imported nodes

Display Feeds Items in Block

Lastly we'll create our block - I'll skip through the details of Views, but basically set up a view block that displays content of type RSS Item:

drupal feeds views block

And then display the title, which I linked to the 'link' field for these RSS item nodes, as well as the description.

drupal feeds set up views block

Display this block on your desired page and this is what you should see:

drupal feeds block with views

Hope this was helpful. Any questions or comments, please leave a note below!

Related Posts

Managing Drupal 7 display modes video

Tom McCracken
Read more

How to Programmatically Create a Quicktab Block in Drupal

Dustin Currie
Read more

Creating Custom Fields In Drupal Using Display Suite

Kyle Taylor
Read more

Facet Block - The Unknown Hero

Kyle Taylor
Read more

Loving the Block Revisited

Randall Knutson
Read more

Fighting Field Labels with Display Suite Extras

Kyle Taylor
Read more