How can I add the user picture of the user who posted a recent comment to this block? (Preferably without modding the core).

Comments

vm’s picture

without modifying core, you would write a custom DB query and add it in administer -> blocks using the php input format.

take a look at some of the php snippets in the php snippets section of the handbook and you will find a few user picture type snippets that you can play with. Do so on a page content type until you get the snippet to work correctly. adding php that is not correct into a block can halt your site forcing you to manually go into the DB and shut the custom block off.

drupalina’s picture

For those of us who are not PHP coders, would someone PLEASE post a working code for such a block?

I can see that there have been many people who wanted to creat such a "recent comments" block with avatars of the commenters. It would be nice to simply have a code that other drupal users could just copy and paste into their custom block.

Many thanks in advance

vm’s picture

no code snippet. One would use the views.module for this in Drupal 6.x if you are using Drupal 5.x you may want to sift through the snippets in the documentation area as I believe there are multiple snippets of this sort these days.

drupalina’s picture

Hi,

It seems like Google is loosing it's edge - you can no longer find _exactly_ what you're looking for simply by entering simple keywords.
Anyway... After googling with some weird word combinations I found the question to my own answer here:
http://drupalsn.com/learn-drupal/drupal-questions/question-3013#comment-506

basically, for Drupal 5.x just enter the following code into your template.php

<?php
function phptemplate_comment_block() {
  $items = array();
  $imagecache_preset = '30px_user_pic'; //replace with your imagecache preset

  foreach (comment_get_recent('8') as $comment) { 
    //get user details so we can get their profile picture
    $account = db_fetch_object(db_query('SELECT u.uid, u.picture, u.name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d', $comment->cid));
  
    //check user has a picture & it exists if not show default image
    if ($account->picture && file_exists($account->picture)) {
      $picture = $account->picture;
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }  
    $user_picture = theme('imagecache', $imagecache_preset, $picture, $account->name, $account->name);
    $user_picture = $user_picture ? l($user_picture, "user/$account->uid", array('title' => $account->name), NULL, NULL, FALSE, true) : '';
 
    //output
    $output = '<div class="comment-list-item list-item">';
    $output .= '<div class="user-picture">'. $user_picture .'</div>';
    $output .= l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid);
    $output .= '<div class="comment-date">('. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp))) .')</div>';
    $output .= '<div style="clear:left;"></div>';
    $output .= '</div>';
    $items[] = $output;
  }

  if ($items) {
    return implode('', $items);
  }
}
?>

Note: don't forget to remove <?php and ?> at the beginning and the end of this code.

Note2: you will also need to enter the exact name of your imagecache preset instead of "30px_user_pic".

Hope this helps the non-coder Drupal users ;)

bryo99’s picture

You can create a custom block under Views.

mokargas’s picture

Sorry I don't follow, how would I get the profile user pics? (can't select under views)