<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(&amp;$vars) {<BR>
&nbsp;&nbsp;$keys = array_keys($vars['elements']['comments']['comments']);<BR>
&nbsp;&nbsp;foreach ($keys as $k) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;if (!is_numeric($k)) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;&nbsp;&nbsp;$pid = $vars['elements']['comments']['comments'][$k]['#comment']-&gt;pid;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;$vars['elements']['comments']['comments'][$pid]['#comment']-&gt;has_children = !empty($pid);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;$last = $k;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;$vars['elements']['comments']['comments'][$last]['#comment']-&gt;last = TRUE;<BR>
}<BR>
<BR>
The last line before closing the function adds an additional flag &#8220;last&#8221; to the comments array on the final comment in the array.<BR>
<BR>
Cameron<BR>
<BR>
<BR>
On 2/16/12 12:10 AM, &quot;Cameron B. Prince&quot; &lt;<a href="cplists@teslauniverse.com">cplists@teslauniverse.com</a>&gt; wrote:<BR>
<BR>
<FONT COLOR="#0000FF">&gt; I have a need to determine if comments have replies. This is what I've come<BR>
&gt; up with to make this determination, but I would rather find a way that<BR>
&gt; doesn't require an additional database query per comment.<BR>
&gt; <BR>
&gt; function theme_preprocess_comment(&amp;$variables) {<BR>
&gt; &nbsp;&nbsp;$comment = $variables['elements']['#comment'];<BR>
&gt; &nbsp;&nbsp;$count = db_query('SELECT COUNT(*) FROM {comment} WHERE pid = :cid',<BR>
&gt; array(':cid' =&gt; $comment-&gt;cid))-&gt;fetchField();<BR>
&gt; &nbsp;&nbsp;$comment-&gt;has_children = !empty($count);<BR>
&gt; }<BR>
&gt; <BR>
&gt; I can see that theme_preprocess_node has access to the entire array of<BR>
&gt; comments in its $variables argument when displaying it with print_r. The<BR>
&gt; issue is I can't seem to figure out how to loop over the comments so that I<BR>
&gt; could check for a value in the pid field and add a has_children =&gt; TRUE to<BR>
&gt; the parent comment's array.<BR>
&gt; <BR>
&gt; Can someone point me in the right direction?<BR>
&gt; <BR>
&gt; Thanks,<BR>
&gt; cameronbprince<BR>
&gt; <BR>
&gt; <BR>
</FONT></SPAN></FONT>
</BODY>
</HTML>