function TermStorage::getNodeTerms

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getNodeTerms()
  2. 10 core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getNodeTerms()
  3. 11.x core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getNodeTerms()

Overrides TermStorageInterface::getNodeTerms

File

core/modules/taxonomy/src/TermStorage.php, line 342

Class

TermStorage
Defines a Controller class for taxonomy terms.

Namespace

Drupal\taxonomy

Code

public function getNodeTerms(array $nids, array $vocabs = [], $langcode = NULL) {
    $query = $this->database
        ->select($this->getDataTable(), 'td');
    $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
    $query->fields('td', [
        'tid',
    ]);
    $query->addField('tn', 'nid', 'node_nid');
    $query->orderby('td.weight');
    $query->orderby('td.name');
    $query->condition('tn.nid', $nids, 'IN');
    $query->addTag('taxonomy_term_access');
    if (!empty($vocabs)) {
        $query->condition('td.vid', $vocabs, 'IN');
    }
    if (!empty($langcode)) {
        $query->condition('td.langcode', $langcode);
    }
    $results = [];
    $all_tids = [];
    foreach ($query->execute() as $term_record) {
        $results[$term_record->node_nid][] = $term_record->tid;
        $all_tids[] = $term_record->tid;
    }
    $all_terms = $this->loadMultiple($all_tids);
    $terms = [];
    foreach ($results as $nid => $tids) {
        foreach ($tids as $tid) {
            $terms[$nid][$tid] = $all_terms[$tid];
        }
    }
    return $terms;
}

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