Is there a way to customize this so it shows the avatar picture on the front page?

Comments

coreyp_1’s picture

You could create a custom block with code like this:

  // Select the $number nodes (visible to the current user) with the most
  // recent comments. This is efficient due to the index on
  // last_comment_timestamp.
  $number = 10;
  $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node_comment_statistics} n WHERE n.comment_count > 0 ORDER BY n.last_comment_timestamp DESC"), 0, $number);

  $nids = array();
  while ($row = db_fetch_object($result)) {
    $nids[] = $row->nid;
  }

  $comments = array();
  if (!empty($nids)) {
    // From among the comments on the nodes selected in the first query,
    // find the $number most recent comments.
    $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp c.uid FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', COMMENT_PUBLISHED, 0, $number);
    while ($comment = db_fetch_object($result)) {
      $comments[] = $comment;
    }
  }

  $items = array();
  foreach ($comments as $comment) {
    $user = user_load(array('uid' => $comment->uid));
    $items[] = ($user->picture ? theme('user_picture', $user) : '') . l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
  }
  if ($items) {
    print theme('item_list', $items);
  }

Make sure the PHP filter is enabled!

Standard disclaimer: Code not tested. Not guaranteed against data loss, teeth loss, hair loss, or weight loss. In the event of weight loss, notify the author, so he can begin using the snippet, too.

- Corey

iweczek’s picture

How would I change the blog hook in blog.module to display user pictures next to the recent blog entries, here is the code that needs to be modified. Please help.

* Implementation of hook_block().
*
* Displays the most recent 10 blog titles.
*/
function blog_block($op = 'list', $delta = 0) {
global $user;
if ($op == 'list') {
$block[0]['info'] = t('Recent blog posts');
return $block;
}
else if ($op == 'view') {
if (user_access('access content')) {
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), 0, 10);
if (db_num_rows($result)) {
$block['content'] = node_title_list($result);
$block['content'] .= '

';
$block['subject'] = t('Recent blog posts');
return $block;
}
}
}
}

rojkii’s picture

how about avatars and username for the recent blog posts block?

-roj

e-boyz’s picture

The first code worked well, but it only shows the avatars of the users who have uploaded a custom avatar, and no shows the default user picture (the picture that Drupal shows when the author did not upload an image).

I have no idea of PHP. How must I change the code for that it shows the default user picture too?

Thanks!

drupalina’s picture

the above block code didn't work for me

Would the Drupal coders please be kind enough to post a code for this PHP block that actually is tested and works, please??? So that people (drupal non-coders) can simply copy and paste from here.

Please also include instructions on what to do. Thanks a lot!

iweczek’s picture

I modified my comments.module to this:

function theme_comment_block() {
$items = array();
foreach (comment_get_recent() as $comment) {
$user = user_load(array('uid' => $comment->uid));
$items[] = ($user->picture ? theme('user_picture', $user) : '') . l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'
'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
}
if ($items) {
return theme('item_list', $items);
}
}

and what I get is the general picture in the comment list. I need to get the picture of the actual commenter. Shouldn't $user be $account?

iweczek’s picture

My problem was that I did not include c.uid in $result. Also, you need a comma after c.timestamp, that is the small syntax error i got.

coreyp_1’s picture

Sorry about the comma. That's what I get for not testing code.

I would encourage you to not change core files, since it will wreak havock on you later when you upgrade. Drupal provides you with a method to create custom blocks, so why not use it?

- Corey