Drupal and blog filtering again

After some struggling over the last couple of days to sort out tag-based blog aggregator filtering in Drupal, here’s how I did it, with the extra patches and sub-modules I needed to make it work.

Installation of modules

First, install the Views, FeedAPI and FeedAPI Item Filter modules for Drupal. Apply the patch from the bottom of this page to fix some issues with using multiple filters in the FeedAPI Item Filter. Then download and install my category filter filter (also attached at the bottom of this page), and unpack the tarball directly in the feedapi_itemfilter module directory. Finally, in Drupal’s module administration page, enable all of the FeedAPI modules.

Node type for posts

Now we have all of the tools, we need to configure them. FeedAPI works by constructing new nodes out of blog entries. To make it easy to display just the blog posts we want (there are lots of other ways of doing this, I’m sure, but this one is simple and works for a small set of filter types), we first create a new content type that will become our blog posts. In Administer > Content management > Content types, add a new content type called Blog Feed Item, and set up the defaults for it (you probably want comments turned off — if people want to comment, they can do so on the original article).

Creating and filtering feeds

The next part of the job is to gather in the blog feeds you want to filter and display. With FeedAPI, you create a Feed node for each blog you want to syndicate, so head off to Create content > Feed and start creating it. Give it a title (I use the name fo the author), leave the body blank, and don’t bother giving it a menu item. This node won’t ever get seen by visitors to the site — it’s just a placeholder to hang on to the details of the blog location and the filtering rules.

You should also tell the Feed what type of node to create. Unfold the Processors section, and under FeedAPI Node - create nodes from feed items, set the Node type of feed items to Blog Feed Item. I would recommend setting Created date of item nodes to Retrieve from feed.

The last thing you’ll need to do is set the filtering rules: unfold the Item filters section, and the Category filter section within that. Enable the filter, and add your filter terms to it, comma-separated. You can supply negative keywords by prefixing with a -. The filter will examine all the posts of the feed, and discard any posts that have any of the negative tags you specify. It will also discard any posts which do not have any of the positive tags. It keeps anything else. If you have Refresh feed on creation set, Drupal will automatically check and filter the feed when you hit Save, and tell you how many items it found and how many it kept.

From the Feed node you’ve just created, you can remove all of the items, re-check (and re-filter) the feed, see the items it’s kept, and link directly to the source blog. You should create a new Feed node for every blog you want to aggregate.

Displaying items

The final part of this odyssey is displaying all of the blog posts on a single page together. We do this with the Views module — one of the most powerful bits of Drupal. Go to Administer > Site building > Views, and start adding a new View. Give it a name (“Blog items”) and an internal name (“blog_items”), with a View type of Node. The View properties screen is a gigantic morass of many options, but fortunately we’ll only need a few of them. We want to create a view which can be treated as a web page in its own right, and which lists only the Blog Feed Item nodes, in the order the posts were created.

We’ll start by filtering the view. Over on the right-hand side of the view configuration page is a section labelled Filters. Click on the little plus in the header of that section, and a list will appear in a panel below the main config. Select Node: Type, and hit Add. Then tell it to select any item which Is one of node type Blog Feed Item, and hit Update.

We should also configure it to display the posts in a sensible way. Under Basic settings, click on Row style: Fields, and change it to Node. Leave the options unchanged. This will display the blog posts in the same style that they would appear on the front page of the site. You can see what the view will look like using the Live preview section near the bottom of the page.

It would be nice to sort items in the order that they were originally posted. In the Sort criteria section on the right of settings box, click the little plus in the header to add a new sort item. Then select FeedAPI Item: Date from the list and Add the criterion. Change the sort order to descending, so that the most recent posts appear at the top of the page, and update it.

The last thing to do is to allow the view to be seen as a single page in its own right. On the left of the view settings area, choose Page from the upper drop-down list, and select Add display. This will add a new set of settings specifically for when the view is seen as a stand-alone page. Select the newly-created Page tab, and you’ll see most of the settings change to italics, indicating that they’re unchanged from the default view settings. However, just under the Basic settings section, you’ll find a new Page settings section. Use the Path setting to define a URL for this view — call it news_page. You can also define its position in your site’s menus here, if you want.

Finally, save your view (don’t forget this bit!), and go to visit your new page, using the View “Page” link above the top right of the settings panel, and marvel in the glory of your newly-made aggregator.