<HTML>
<HEAD>
<TITLE>Re: [support] Determine if comments have replies</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>After much trial and error (and temptation to edit comments.module), I was able to figure out how to determine if comments have replies without additional queries. Since the theme_preprocess_comment function runs per comment and does not have access to the entire array of comments, the code must be placed in theme_preprocess_node as follows:<BR>
<BR>
function theme_preprocess_node(&$vars) {<BR>
$keys = array_keys($vars['elements']['comments']['comments']);<BR>
foreach ($keys as $k) {<BR>
if (!is_numeric($k)) {<BR>
continue;<BR>
}<BR>
$pid = $vars['elements']['comments']['comments'][$k]['#comment']->pid;<BR>
$vars['elements']['comments']['comments'][$pid]['#comment']->has_children = !empty($pid);<BR>
$last = $k;<BR>
}<BR>
$vars['elements']['comments']['comments'][$last]['#comment']->last = TRUE;<BR>
}<BR>
<BR>
The last line before closing the function adds an additional flag “last” to the comments array on the final comment in the array.<BR>
<BR>
Cameron<BR>
<BR>
<BR>
On 2/16/12 12:10 AM, "Cameron B. Prince" <<a href="cplists@teslauniverse.com">cplists@teslauniverse.com</a>> wrote:<BR>
<BR>
<FONT COLOR="#0000FF">> I have a need to determine if comments have replies. This is what I've come<BR>
> up with to make this determination, but I would rather find a way that<BR>
> doesn't require an additional database query per comment.<BR>
> <BR>
> function theme_preprocess_comment(&$variables) {<BR>
> $comment = $variables['elements']['#comment'];<BR>
> $count = db_query('SELECT COUNT(*) FROM {comment} WHERE pid = :cid',<BR>
> array(':cid' => $comment->cid))->fetchField();<BR>
> $comment->has_children = !empty($count);<BR>
> }<BR>
> <BR>
> I can see that theme_preprocess_node has access to the entire array of<BR>
> comments in its $variables argument when displaying it with print_r. The<BR>
> issue is I can't seem to figure out how to loop over the comments so that I<BR>
> could check for a value in the pid field and add a has_children => TRUE to<BR>
> the parent comment's array.<BR>
> <BR>
> Can someone point me in the right direction?<BR>
> <BR>
> Thanks,<BR>
> cameronbprince<BR>
> <BR>
> <BR>
</FONT></SPAN></FONT>
</BODY>
</HTML>