You are here

function sitemap_taxonomy_term_count_nodes in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 sitemap.module \sitemap_taxonomy_term_count_nodes()
  2. 8 sitemap.module \sitemap_taxonomy_term_count_nodes()

Count the number of published nodes classified by a term.

This is a re-implementation of taxonomy_term_count_nodes() that has been removed from D7 core.

Implementation note: the normal way to count field instances is through field_attach_query(), but taxonomy.module has a special denormalized table taxonomy_index which we can use for more speed. THX to taxonews.

Parameters

string $tid: The term's ID.

Return value

string An integer representing a number of nodes. Results are statically cached.

1 call to sitemap_taxonomy_term_count_nodes()
Vocabulary::checkTermThreshold in src/Plugin/Sitemap/Vocabulary.php
Checks the threshold count for a term.

File

./sitemap.module, line 101
Provides sitemap functionality.

Code

function sitemap_taxonomy_term_count_nodes($tid) {
  $query = \Drupal::database()
    ->select('taxonomy_index', 'ti');
  $query
    ->addExpression('COUNT(ti.nid)');
  $count = $query
    ->condition('ti.tid', $tid)
    ->execute()
    ->fetchCol();
  return $count[0];
}