You are here

public function CommentStorage::getNewCommentPageNumber in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/comment/src/CommentStorage.php \Drupal\comment\CommentStorage::getNewCommentPageNumber()
  2. 9 core/modules/comment/src/CommentStorage.php \Drupal\comment\CommentStorage::getNewCommentPageNumber()

Calculates the page number for the first new comment.

Parameters

int $total_comments: The total number of comments that the entity has.

int $new_comments: The number of new comments that the entity has.

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity to which the comments belong.

string $field_name: The field name on the entity to which comments are attached.

Return value

array|null The page number where first new comment appears. (First page returns 0.)

Overrides CommentStorageInterface::getNewCommentPageNumber

File

core/modules/comment/src/CommentStorage.php, line 147

Class

CommentStorage
Defines the storage handler class for comments.

Namespace

Drupal\comment

Code

public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
  $field = $entity
    ->getFieldDefinition($field_name);
  $comments_per_page = $field
    ->getSetting('per_page');
  $data_table = $this
    ->getDataTable();
  if ($total_comments <= $comments_per_page) {

    // Only one page of comments.
    $count = 0;
  }
  elseif ($field
    ->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {

    // Flat comments.
    $count = $total_comments - $new_comments;
  }
  else {

    // Threaded comments.
    // 1. Find all the threads with a new comment.
    $unread_threads_query = $this->database
      ->select($data_table, 'comment')
      ->fields('comment', [
      'thread',
    ])
      ->condition('entity_id', $entity
      ->id())
      ->condition('entity_type', $entity
      ->getEntityTypeId())
      ->condition('field_name', $field_name)
      ->condition('status', CommentInterface::PUBLISHED)
      ->condition('default_langcode', 1)
      ->orderBy('created', 'DESC')
      ->orderBy('cid', 'DESC')
      ->range(0, $new_comments);

    // 2. Find the first thread.
    $first_thread_query = $this->database
      ->select($unread_threads_query, 'thread');
    $first_thread_query
      ->addExpression('SUBSTRING([thread], 1, (LENGTH([thread]) - 1))', 'torder');
    $first_thread = $first_thread_query
      ->fields('thread', [
      'thread',
    ])
      ->orderBy('torder')
      ->range(0, 1)
      ->execute()
      ->fetchField();

    // Remove the final '/'.
    $first_thread = substr($first_thread, 0, -1);

    // Find the number of the first comment of the first unread thread.
    $count = $this->database
      ->query('SELECT COUNT(*) FROM {' . $data_table . '} WHERE [entity_id] = :entity_id
                        AND [entity_type] = :entity_type
                        AND [field_name] = :field_name
                        AND [status] = :status
                        AND SUBSTRING([thread], 1, (LENGTH([thread]) - 1)) < :thread
                        AND [default_langcode] = 1', [
      ':status' => CommentInterface::PUBLISHED,
      ':entity_id' => $entity
        ->id(),
      ':field_name' => $field_name,
      ':entity_type' => $entity
        ->getEntityTypeId(),
      ':thread' => $first_thread,
    ])
      ->fetchField();
  }
  return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0;
}