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.

Filed under: Design, Drupal | Tags: , , , , , | Written on October 14, 2008

9 Comments »

Comment by
Zoe

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?

October 23, 2008 @ 4:13 am

Comment by
Ryan

@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.

October 23, 2008 @ 10:06 am

Comment by
Alex

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.

October 23, 2008 @ 7:59 pm

Comment by
simon

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.

November 13, 2008 @ 6:31 am

Comment by
Laseacasp

Interesting and communicative, but would participate in something more on this topic?

February 11, 2009 @ 11:35 am

Comment by
Padma

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

May 27, 2009 @ 2:58 pm

Comment by
line

tnks you

November 8, 2009 @ 6:19 pm

Comment by
Acez

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

February 8, 2010 @ 8:31 am

Comment by
jack

thanks helped a lot

February 8, 2010 @ 6:06 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

PIGEONholed

RECENTLYspoken

  • voiture location Said: the link is broken plz fixed.thank you for this topic
  • Ed Said: Very nice looking site. Have you set your Home Page to the Front Page by any chance?
  • Jo Said: Hi, thank for plugin, it is Greit! Will be possible to use diferent Optional Front Page Title for diferent...
  • masazumi kawauchi Said: give me virus! m_kawauchi@enzantrades.jp
  • Rob Cubbon Said: Very, very useful post. I used this code to do a “magazine” type home page where the...

BLOGroll

PASTposts

BADGEpatch

Blog Directory - Blogged Internet Blogs - BlogCatalog Blog Directory
Copyright © 2008 Dao By Design, All Rights Reserved.