1 comment.module | comment_prepare_thread(&$comments) |
Calculates the indentation level of each comment in a comment thread.
This function loops over an array representing a comment thread. For each comment, the function calculates the indentation level and saves it in the 'divs' property of the comment object.
Parameters
array $comments: An array of comment objects, keyed by comment ID.
File
- core/
modules/ comment/ comment.module, line 912 - Enables users to comment on published content.
Code
function comment_prepare_thread(&$comments) {
// A flag stating if we are still searching for first new comment on the thread.
$first_new = TRUE;
// A counter that helps track how indented we are.
$divs = 0;
foreach ($comments as $key => $comment) {
if ($first_new && $comment->new != MARK_READ) {
// Assign the anchor only for the first new comment. This avoids duplicate
// id attributes on a page.
$first_new = FALSE;
$comment->first_new = TRUE;
}
// The $divs element instructs #prefix whether to add an indent div or
// close existing divs (a negative value).
$comment->depth = count(explode('.', $comment->thread)) - 1;
if ($comment->depth > $divs) {
$comment->divs = 1;
$divs++;
}
else {
$comment->divs = $comment->depth - $divs;
while ($comment->depth < $divs) {
$divs--;
}
}
$comments[$key] = $comment;
}
// The final comment must close up some hanging divs
$comments[$key]->divs_final = $divs;
}