I really like the idea of showing the 'Recent comments' block... so far I have modified it to show just the Subject and the full comment. However, some comments are very very long.

Would somebody be able to add a bit of code which shows the first 6 words of a comment?

Here is the code so far....

/**
* Implementation of hook_block().
*
* Generates a block with the most recent comments.
*/
function comment_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Recent comments');
return $blocks;
}
else if (user_access('access comments')) {
$result = db_query_range('SELECT * FROM {comments} WHERE status = 0 ORDER BY timestamp DESC', 0, 10);
$items = array();
while ($comment = db_fetch_object($result)) {
$items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) . ': '. t($comment->comment) ;
}

$block['subject'] = t('Recent comments...');
$block['content'] = theme('item_list', $items);
return $block;
}
}

thanks, Jonathan

Jonathan Furness
www.jonathansblog.net

Comments

dries’s picture

You can use truncate_utf8() to truncate a string after x characters (not words). If you are using Drupal HEAD, you can set the third parameter (not available in Drupal 4.5) to TRUE so it's word-safe and truncates at word boundaries.

PS: you don't want to wrap a call to t() around $comment->comment.

Jonathan Furness’s picture

Thanks Dries.... I have added the truncate_utf8 function... sadly I am not using Drupal HEAD...

Cheers for the correction 't()' - now removed...

J

Jonathan Furness
teacher, developer, webmaster
http://www.jonathansblog.net