function CommentStorage::getDisplayOrdinal

Same name in other branches
  1. 8.9.x core/modules/comment/src/CommentStorage.php \Drupal\comment\CommentStorage::getDisplayOrdinal()
  2. 10 core/modules/comment/src/CommentStorage.php \Drupal\comment\CommentStorage::getDisplayOrdinal()
  3. 11.x core/modules/comment/src/CommentStorage.php \Drupal\comment\CommentStorage::getDisplayOrdinal()

Overrides CommentStorageInterface::getDisplayOrdinal

File

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

Class

CommentStorage
Defines the storage handler class for comments.

Namespace

Drupal\comment

Code

public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
    // Count how many comments (c1) are before $comment (c2) in display order.
    // This is the 0-based display ordinal.
    $data_table = $this->getDataTable();
    $query = $this->database
        ->select($data_table, 'c1');
    $query->innerJoin($data_table, 'c2', '[c2].[entity_id] = [c1].[entity_id] AND [c2].[entity_type] = [c1].[entity_type] AND [c2].[field_name] = [c1].[field_name]');
    $query->addExpression('COUNT(*)', 'count');
    $query->condition('c2.cid', $comment->id());
    if (!$this->currentUser
        ->hasPermission('administer comments')) {
        $query->condition('c1.status', CommentInterface::PUBLISHED);
    }
    if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
        // For rendering flat comments, cid is used for ordering comments due to
        // unpredictable behavior with timestamp, so we make the same assumption
        // here.
        $query->condition('c1.cid', $comment->id(), '<');
    }
    else {
        // For threaded comments, the c.thread column is used for ordering. We can
        // use the sorting code for comparison, but must remove the trailing
        // slash.
        $query->where('SUBSTRING([c1].[thread], 1, (LENGTH([c1].[thread]) - 1)) < SUBSTRING([c2].[thread], 1, (LENGTH([c2].[thread]) - 1))');
    }
    $query->condition('c1.default_langcode', 1);
    $query->condition('c2.default_langcode', 1);
    $ordinal = $query->execute()
        ->fetchField();
    return $divisor > 1 ? floor($ordinal / $divisor) : $ordinal;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.