You are here

function flat_comments_calculate_thread in Flatcomments 8

By the time we reset the `pid` of the comment, the thread has been already calculated, so in order to make sure that the comments are completely flattened we need to recalculate the thread.

This code was mainly taken from Drupal\comment\Entity\Comment::preSave();

1 call to flat_comments_calculate_thread()
flat_comments_comment_presave in ./flat_comments.module
Implementation of hook_comment_presave().

File

./flat_comments.module, line 96
Contains flat_comments.module.

Code

function flat_comments_calculate_thread($comment) {
  $comment_storage = \Drupal::entityTypeManager()
    ->getStorage('comment');

  // This is a comment with no parent comment (depth 0): we start
  // by retrieving the maximum thread level.
  $max = $comment_storage
    ->getMaxThread($comment);

  // Strip the "/" from the end of the thread.
  $max = rtrim($max, '/');

  // We need to get the value at the correct depth.
  $parts = explode('.', $max);
  $n = Number::alphadecimalToInt($parts[0]);
  return Number::intToAlphadecimal(++$n) . '/';
}