You are here

protected function ForumManager::getForumStatistics in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/forum/src/ForumManager.php \Drupal\forum\ForumManager::getForumStatistics()
  2. 9 core/modules/forum/src/ForumManager.php \Drupal\forum\ForumManager::getForumStatistics()

Provides statistics for a forum.

Parameters

int $tid: The forum tid.

Return value

object|null Statistics for the given forum if statistics exist, else NULL.

File

core/modules/forum/src/ForumManager.php, line 385

Class

ForumManager
Provides forum manager service.

Namespace

Drupal\forum

Code

protected function getForumStatistics($tid) {
  if (empty($this->forumStatistics)) {

    // Prime the statistics.
    $query = $this->connection
      ->select('node_field_data', 'n');
    $query
      ->join('comment_entity_statistics', 'ces', "[n].[nid] = [ces].[entity_id] AND [ces].[field_name] = 'comment_forum' AND [ces].[entity_type] = 'node'");
    $query
      ->join('forum', 'f', '[n].[vid] = [f].[vid]');
    $query
      ->addExpression('COUNT([n].[nid])', 'topic_count');
    $query
      ->addExpression('SUM([ces].[comment_count])', 'comment_count');
    $this->forumStatistics = $query
      ->fields('f', [
      'tid',
    ])
      ->condition('n.status', 1)
      ->condition('n.default_langcode', 1)
      ->groupBy('tid')
      ->addTag('node_access')
      ->execute()
      ->fetchAllAssoc('tid');
  }
  if (!empty($this->forumStatistics[$tid])) {
    return $this->forumStatistics[$tid];
  }
}