You are here

public static function FaqHelper::taxonomyTermCountNodes in Frequently Asked Questions 8

Count number of nodes for a term and its children.

Parameters

int $tid: Id of the tadonomy term to count nodes in.

Return value

int Returns the count of the nodes in the given term.

11 calls to FaqHelper::taxonomyTermCountNodes()
FaqCategoriesBlock::build in src/Plugin/Block/FaqCategoriesBlock.php
Implements \Drupal\block\BlockBase::blockBuild().
FaqController::faqPage in src/Controller/FaqController.php
Function to display the faq page.
FaqController::_getIndentedFaqTerms in src/Controller/FaqController.php
Return a structured array that consists a list of terms indented according to the term depth.
FaqHelper::getChildCategoriesFaqs in src/FaqHelper.php
Helper function for retrieving the sub-categories faqs.
FaqHelper::viewChildCategoryHeaders in src/FaqHelper.php
Helper function to setup the list of sub-categories for the header.

... See full list

File

src/FaqHelper.php, line 52

Class

FaqHelper
Contains static helper functions for FAQ module.

Namespace

Drupal\faq

Code

public static function taxonomyTermCountNodes($tid) {
  static $count;
  if (!isset($count) || !isset($count[$tid])) {
    $query = \Drupal::database()
      ->select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->addTag('node_access');
    $query
      ->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
    $query
      ->join('node_field_data', 'd', 'd.nid = n.nid');
    $query
      ->condition('n.type', 'faq')
      ->condition('d.status', 1)
      ->condition('ti.tid', $tid);
    $count[$tid] = $query
      ->countQuery()
      ->execute()
      ->fetchField();
  }
  $children_count = 0;
  foreach (FaqHelper::taxonomyTermChildren($tid) as $child_term) {
    $children_count += FaqHelper::taxonomyTermCountNodes($child_term);
  }
  return $count[$tid] + $children_count;
}