4

The following is part of the comment.module file (line 950):

// Add indentation div or close open divs as needed.
if ($is_threaded) {
  $prefix .= $comment->divs <= 0 ? str_repeat('</div>', abs($comment->divs)) : "\n"     . '<div class="indented">';
}

Can someone explain that what is it doing?

1 Answer 1

4

For reference, this is in the comment_view() function.

It is equivalent to this, which makes it a little easier to understand:

if ($is_threaded) {
  if ($comment->divs <= 0) {
    $prefix .= str_repeat('</div>', abs($comment->divs));
  }
  else {
    $prefix .= "\n" . '<div class="indented">';
  }
}

$is_threaded is a boolean value for whether or not threading has been enabledfor comments in the settings for this content type.

$comment->divs is set in comment_prepare_thread() and is a count of the indentation divs required to have this comment in the right place in terms of the comment's depth. So this value will either be 1 if a new indentation div needs to be added to make sure the comment is at the correct depth, or a negative number, which is how many indentation divs need to be closed to have the comment at the correct depth.

So if comment threading is enabled, then if $comment->divs is less than or equal to zero then close zero or more indentation divs by appending str_repeat('</div>', abs($comment->divs)) to the markup otherwise, if $comment->divs is 1 then open a new indentation div by adding '<div class="indented">' to the markup.

For example if you had these comments:

Comment 1
-Comment 2
--Comment 3
---Comment 4
--Comment 5
---Comment 6
----Comment 7
-----Comment 8
-----Comment 9
Comment 10
Comment 11
-Comment 12
Comment 13
Comment 14

Then for each comment, $comment->divs would be:

  • Comment 1: 0
  • Comment 2: 1
  • Comment 3: 1
  • Comment 4: 1
  • Comment 5: -1
  • Comment 6: 1
  • Comment 7: 1
  • Comment 8: 1
  • Comment 9: 0
  • Comment 10: -5
  • Comment 11: 0
  • Comment 12: 1
  • Comment 13: -1
  • Comment 14: 0
3
  • Thank you so much. So just let me see if I have understood it. For example if $comment->div is equal to -5, it means that 5 threaded comments were made, so have to add 5 </div>tag to close each of them, so they will be displayed as thread comments. But if $comment->div is equal to 1, it means it is not a threaded comment and is a new comment, so just have to open <div> with class="indented" for it.am I right? Apr 2, 2015 at 5:27
  • If $comment->divs is 1, then it means that the comment is a reply to the previous comment and should be indented. If it is -5 it means that the previous comment was 5 levels deeper than the current comment so 5 indentation divs need closing. For example if comments 2 to 6 are all nested replys to comment 1 then you are 5 levels deep of indenting. If comment 7 is then another top level comment then it should not be indented so $comment->divs will be -5 because the previous 5 indenting divs need closing to get back to the top level.
    – rooby
    Apr 2, 2015 at 5:32
  • I added an example if that helps.
    – rooby
    Apr 2, 2015 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.