Coding A Custom Comments Listing Block
Welcome Geeks and God listeners. On this past episode of the Geeks and God podcast, Bob and I said we would start blogging about drupal, the popular open source content management system, and design and development in general. As we said on the podcast, these design and development posts will be about once a week. To help with finding and sorting these posts on MattFarina.com there are new design and development categories. So are you ready to dive into something that combines design, development, and drupal?
Note: You can find Bob blogging about this at mustardseedmedia.com/blog.
Over on the Geeks and God homepage we have a block that has a list of recent user comments with the time they were posted. This is different from the the default user comments block in drupal which shows you how long ago a comment was posted. While the default comments block in drupal is great, once caching is enabled the time since the comment was posted doesn't display correctly to anonymous visitors. And, if you are using the Block Cache module signed in users may have the same problem. This is the problem, lets look at a solution.
My first gut reaction was to make a block using the views module that would display the time the comment was submitted rather than how long ago. While this is a possible solution, I didn't think it was the best solution. I started asking myself what about performance? Is there a way to theme the comments block that's already there?
A better solution is to theme the comments block that we already have. Doing this keeps the presentation of the block tied to the theme, doesn't need the overhead of views, and best of all gets us to do a little development to support that design.
The content area of the comments block is themed by the function theme_comment_block. This is important becuase if I want to override this theming all I have to do it create a function called phptemplate_comment_block, place this function in my phptemplate themes template.php file, and my comments block will be themed the way I decide in that function.
Lets start by creating our phptemplate_comment_block function by copying the content of the theme_comment_block and altering it to suit our needs. This function would look like:
<?php
function phptemplate_comment_block() {
$items = array();
foreach (comment_get_recent() as $comment) {
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
}
if ($items) {
return theme('item_list', $items);
}
}
?>To change the time information the part we want to change is:
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));To change this to read the time it was posted we change this to:
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time', array('@time' => format_date($comment->timestamp, 'custom', 'n/j/y - g:ia')));This is changing the '@time ago' to '@time' which removes the ago from the time and it changes the time generation from using format_interval function used to generate intervals to using the format_date function to create a date. The important thing about this is where it says 'n/j/y - g:ia'. This is what tells it how to display the time and date. For more information on how to configure the date and time display check out this information at php.net.
So, now our function that shows the date and time it was posted reads:
<?php
function phptemplate_comment_block() {
$items = array();
foreach (comment_get_recent($number) as $comment) {
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time', array('@time' => format_date($comment->timestamp, 'custom', 'n/j/y - g:ia')));
}
if ($items) {
return theme('item_list', $items);
}
}
?>Note: This will display the time for the timezone of the server for anonymous users or the timezone of the logged in users.
If would like to use no time information at all the code would be:
<?php
function phptemplate_comment_block() {
$items = array();
foreach (comment_get_recent($number) as $comment) {
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid);
}
if ($items) {
return theme('item_list', $items);
}
}
?>This is just a simple code snippet to theme the comment block we already have. Again, look for more design and development posts each week. Thanks for visiting.
Comments
#1 theme specific function
You could also define the function customthemename_comment_block() rather than phptemplate_comment_block() to change the display for your custom theme only.