I’ve been using Drupal quite a lot recently, and one of the things that was bothering me was the Recent Comments block. It’s excellent that Drupal includes this functionality right out of the box, but that it offers no way to template or otherwise customize it (in Drupal 5.x at least) is a pain in the ass.
Specifically I want to display the comment’s author (as a link) and be able to customize the number of recent comments that are displayed.
A search of Drupal.org revealed a number of comments.module hacks, but I hate hacking core modules and particularly try to avoid it on client sites.
Fortunately, the rather amazing Views module will allow me to do exactly what I need. Here’s how to implement a (more) customized Recent Comments block using the Views module (click to enlarge any of the screenshots):
- Download, install and activate the Views module (just as you would any Drupal module).
- Under the SITE BUILDING (/admin/build) menu you’ll now have a “Views” option. Click it.
- You should see a list of Default Views. We’re going to override the “comments_recent” default view by creating a new view with the same name. Click the ADD tab and insert comments_recent as the name, like so:
- Next, scroll down to the BLOCK sub-section and fill out like so:
You can change the Title field to whatever you’d like the block to display as its title. Also, adjust the Nodes per Block setting to the number of recent comments you wish to display. - Now we need to tell Views which fields to display. You can configure this a number of different ways, but I’m going to stick with my original purpose – getting the author’s name displayed:
You’ll notice that I’ve set some labels for the 2nd and 3rd fields. I’ve used this to add “said:” and ” – ” to the block. - The final thing we need to add is a Sort Criteria. Scroll down to the Sort Criteria section and add a new criteria called “Comment – Created Time”, then set the order to “Descending”, as so:
This assumes you wish to show the most recent comments at the top. If you want to go from oldest to newest, select “Ascending”. - Click SAVE.
The only step left is replacing the Recent Comments block with the new block. Go to your Blocks page (/admin/build/block) and you should see a block called “comments_recent”. Select the region where you want the block displayed, save, and you should see the new Recent Comments block appear on your site.
Optionally, you might want to style things a bit differently. The new Views-generated block puts each field you inserted into its own DIV, making it pretty easy to adjust things with a bit of CSS. Here’s the CSS I used:
#block-views-comments_recent .view-item-comments-recent div {display:inline;}
This makes the individual DIVs for each field (the name, the subject, and the field labels) run as a continuous line, rather than the (default) block each on its own line. For additional styling, simply apply CSS styles to the relevant classes (view the source or use a dom inspector).
Well, I hope that helps any out there who, like me, need a more versatile and customizable Recent Comments block in Drupal.
11