You are here

public function CommentNodeFormatter::loadThread in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  2. 8 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  3. 8.2 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  4. 8.3 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  5. 8.4 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  6. 8.5 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  7. 8.6 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  8. 8.7 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  9. 8.8 modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  10. 10.3.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  11. 10.0.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()
  12. 10.1.x modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php \Drupal\social_core\Plugin\Field\FieldFormatter\CommentNodeFormatter::loadThread()

See also

Drupal\comment\CommentStorage::loadThead()

File

modules/social_features/social_core/src/Plugin/Field/FieldFormatter/CommentNodeFormatter.php, line 204

Class

CommentNodeFormatter
Provides a node comment formatter.

Namespace

Drupal\social_core\Plugin\Field\FieldFormatter

Code

public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {

  // @todo Refactor this to use CommentDefaultFormatter->loadThread with dependency injection instead.
  $query = \Drupal::database()
    ->select('comment_field_data', 'c');
  $query
    ->addField('c', 'cid');
  $query
    ->condition('c.entity_id', $entity
    ->id())
    ->condition('c.entity_type', $entity
    ->getEntityTypeId())
    ->condition('c.field_name', $field_name)
    ->condition('c.default_langcode', 1)
    ->isNull('c.pid')
    ->addTag('entity_access')
    ->addTag('comment_filter')
    ->addMetaData('base_table', 'comment')
    ->addMetaData('entity', $entity)
    ->addMetaData('field_name', $field_name);
  if (!$this->currentUser
    ->hasPermission('administer comments')) {
    $query
      ->condition('c.status', CommentInterface::PUBLISHED);
  }
  if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
    $query
      ->orderBy('c.cid', 'DESC');
  }
  else {

    // See comment above. Analysis reveals that this doesn't cost too
    // much. It scales much much better than having the whole comment
    // structure.
    $query
      ->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
    $query
      ->orderBy('torder', 'DESC');
  }

  // Limit The number of results.
  if ($comments_per_page) {
    $query
      ->range(0, $comments_per_page);
  }
  $cids = $query
    ->execute()
    ->fetchCol();
  $comments = [];
  if ($cids) {
    $comments = $this->storage
      ->loadMultiple($cids);
  }
  return $comments;
}