I'm trying to theme the "Recent Comment Block".

My template.php function looks like:

function aplvncc_comment_block() {
  $items = array();
  $number = variable_get('comment_block_count', 10);
  $comments = comment_get_recent($number);
  
  /*print '<pre>';
  var_dump($comments);
  print '</pre>';*/
  
  for ($i=0; $i < sizeof($comments); $i++) {
	$items[$i] = array();
	$items[$i]['subject'] = $comments[$i]->subject;
	$items[$i]['cid'] = $comments[$i]->cid;
	$items[$i]['uid'] = $comments[$i]->uid;
	$items[$i]['user_name'] = $comments[$i]->name;
	$items[$i]['link'] = 'comment/' . $comments[$i]->cid;
	$items[$i]['posted'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comments[$i]->changed)));
  }
  
  /*print '<pre>';
  var_dump($items);
  print '</pre>';*/
  
  if (!empty($items)) {
    return $items;
  }
  else {
    return t('No comments available.');
  }
}

My block--comment.tpl.php has:

<?php 
            
                //print '<pre>';
					//var_dump($elements);
                    //var_dump(get_defined_vars());
                //print '</pre>';
                
                $comments = $elements;
                
                for($i=0; $i < sizeof($comments); $i++) {
                    print '<div class="recent-comment-subject">' . $comments[$i]['subject'] . '</div>';
                    print '<div class="recent-comment-postedby">' . $comments[$i]['user_name'] . '</div>';
                    print '<div class="recent-comment-posted">' . $comments[$i]['posted'] . '</div>';
                }
            
            ?>

In template.php the array elements look fine. However, when I'm working with them in the block--comment.tpl.php my "subject" and "user_name" content changes.

It should display:

This yet another test subject
Admin
18 min 2 sec ago

but instead is removing the first character and replacing it with a "1".

1his yet another test subject
1dmin
18 min 2 sec ago

Am I accessing this content wrong in the block--comment.tpl.php file? It's wrong when I dump the array in this file but is correct when I dump it in the template.php file. Am I passing it wrong?

Thanks for the help.

Comments

nevets’s picture

What is aplvncc_comment_block(), how is it called?

E Johnson’s picture

It's called in the template.php file. That's how you override the default theme_comment_block() in D7. 'aplvncc' is my custom theme.

https://api.drupal.org/api/drupal/modules!comment!comment.module/functio...

E Johnson’s picture

You got me thinking that, that function is rather generic and not specifically for "Recent Comments".

I then realized the Recent Comment block is created from a View, so I'll just add the fields I need and theme it that way.

Thanks for opening my eyes.