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:
We'll need the following modules:
- Feeds (download this and enable both Feeds and Feeds_UI)
- Job Scheduler
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.
Next create your RSS 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.
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.
We'll now configure Feeds to import at admin/structure/feeds by clicking on Add Importer, giving our importer a name, and clicking Create.
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.
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.
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.
Now, when we run cron, we'll see that Feeds will import new nodes.
If we go to the content overview page, we'll see that 9 new RSS Item nodes have been created.
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:
And then display the title, which I linked to the 'link' field for these RSS item nodes, as well as the description.
Display this block on your desired page and this is what you should see:
Hope this was helpful. Any questions or comments, please leave a note below!