function ForumManager::getForumStatistics

Same name in other branches
  1. 8.9.x core/modules/forum/src/ForumManager.php \Drupal\forum\ForumManager::getForumStatistics()
  2. 10 core/modules/forum/src/ForumManager.php \Drupal\forum\ForumManager::getForumStatistics()
  3. 11.x 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.

1 call to ForumManager::getForumStatistics()
ForumManager::getChildren in core/modules/forum/src/ForumManager.php
Utility method to fetch the child forums for a given forum.

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];
    }
}

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