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