Drupal Block Snippet: Recent Comments by
Posted: Wed 05/02/2007 by ramiroThe Drupal core comment module comes with a block called Recent Comments, that displays links to the latest comments, the comment author's name and the time that passed since the comment was posted.
If you configure the comment module so that users can enter their homepage URL the comment author's name displayed in the comment block is not a link to the URL entered.
Considering the huge amount of spam comments blogs receive this seems perfectly reasonably. But you can configure Drupal so that comments by anonymous user's must be approved. This way you can prevent spam comments from being displayed publicly.
Obviously good comments add value to your site so you can give something back to the comment author if you link his name to the entered homepage URL in a recent comments block without using the nofollow attribute.
The Drupal block snippet below displays such a block with backlinks to the author's homepage with the author name restricted to a maximum length of 15 characters.
<?php
global $user;
$sql = 'SELECT u.uid, c.name, c.nid, c.subject, c.cid, c.timestamp, c.homepage, n.title FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid INNER JOIN {users} u ON c.uid = u.uid WHERE n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC';
$result = db_query_range(db_rewrite_sql($sql, 'c'), COMMENT_PUBLISHED, 0, 5);
$items = array();
while ($comment = db_fetch_object($result)) {
$name = $comment->name;
if (strlen($name) > 15) {
$name = substr($name,0,14);
}
if ($comment->uid > 0 && user_access('access user profiles', $user)) {
$author = l($name, 'user/'. $comment->uid);
} else if ($comment->homepage) {
$author = l($name, $comment->homepage, array(), NULL, NULL, TRUE, FALSE);
} else {
$author = $name;
}
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid, array('title' => $comment->title)) .t(' by '). $author;
}
return theme('item_list', $items);
?>This block is used on SEO Expert Blog. Feel free to use the code on your Drupal site.

You can, for example, use the following code to have the time interval between now and the comment being posted printed on the next line.
<?php $items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid, array('title' => $comment->title)) .t(' by '). $author . '' .t('Posted @time ago', array('@time' => format_interval(time() - $comment->timestamp)); ?>
This is done similar to Drupal's core comment block theme function implementation.
The comment_get_recent() function doesn't return information on the user.
Add the following code to your template.php:
<?phpfunction THEME_NAME_preprocess_node(&$variables) {
$node = $variables['node'];
// Separate out comment link.
if (isset($node->links['comment_comments'])) {
$variables['comment_link'] = l($node->links['comment_comments']['title'],
"node/$node->nid", array('rel' => 'nofollow',
'title' => t('Comments on post.')),
NULL, 'commentblock');
}
elseif (isset($node->links['comment_add'])) {
$variables['comment_link'] = l(t('Your opinion'),
"comment/reply/$node->nid", array('rel' => 'nofollow',
'title' => t('Be the first to comment.')), NULL, 'comment-form');
}
// Kill comment links that are already added above.
if (isset($node->links)) {
$links = $node->links;
if (isset($links['comment_comments'])) {
unset($links['comment_comments']);
}
if (isset($links['comment_add'])) {
unset($links['comment_add']);
}
$variables['links'] = theme('links', $links, array('class' => 'links inline'));
}
}
?>
Then you can add the comment link in your node.tpl.php like that:
<?php if($comment_link) { ?><span class="comment-link"><?php print $comment_link; ?></span>
<?php } ?>
In template.php the variable $comment_link is made available to node templates, e.g. node.tpl.php, and the comment links contained in the $links variable are unset to not show the comment links twice. The code that goes into node.tpl.php is copied from my own theme. Looks like it's working ;)
<?php print_r($node->links)?><?php if ($page == 0): ?> <span class="comments"> <a href="<?php print $node_url ?>" title="Permalink"><?php print $comment_count.' Comment'; if ($comment_count != 1) print 's'; ?></a> </span> <?php endif; ?>Post new comment