Customize Drupal's Recent Comments block

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):

  1. Download, install and activate the Views module (just as you would any Drupal module).
  2. Under the SITE BUILDING (/admin/build) menu you’ll now have a “Views” option. Click it.
  3. 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:
  4. 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.
  5. 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.
  6. 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”.
  7. 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 thoughts on Customize Drupal's Recent Comments block

  1. I love it when people go to the length of making their tutorials this complete 🙂

    Question: What happens if we want to take it one step further, and limit this view to only show comments by one specific user?

  2. @Zoe: Cheers. As for just showing the comments of a specific user – I’m not 100% sure it’s possible, but the Views module does give you some filtering options, and that may be one of them. It will be on the same Views configuration page as above.

  3. One thing to also check is the ‘only published comments’ and ‘only published posts’ are checked as options, unless you want links to unapproved and/or half-finished posts.

    To filter by a particular user… depends on how you’re doing it. If it’s on a page where their user ID or name is in the URL a simple argument is fine. If it’s on content that’s related to them (like, in the ‘nid’ object they’re the author) the argument can be passed from the object.

  4. Thank you, very useful for sorting out a problem with alek 2.0 theme where comments overflowed at bottom of theme, now limited to just 5 comments.

  5. If I don’t want to show unpublished comment with existing recent comment block
    What should I do ?

  6. I was having he same problem. Improved it using your tutorial.very helpful!! thanks

  7. Awesome. Now I can customize my Drupal recent comments module. But one thing that I am worried about is the spam comments.

    People start commenting since I have a recent comments option, with some spam content.

Say something...

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Return to Top ▲Return to Top ▲