TermStorage::getNodeTerms

public TermStorage::getNodeTerms(array $nids, array $vocabs = array(), $langcode = NULL)

Returns all terms used to tag some given nodes.

Parameters

array $nids: Node IDs to retrieve terms for.

array $vocabs: (optional) A vocabularies array to restrict the term search. Defaults to empty array.

string $langcode: (optional) A language code to restrict the term search. Defaults to NULL.

Return value

array An array of nids and the term entities they were tagged with.

Overrides TermStorageInterface::getNodeTerms

File

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

Class

TermStorage
Defines a Controller class for taxonomy terms.

Namespace

Drupal\taxonomy

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public function getNodeTerms(array $nids, array $vocabs = array(), $langcode = NULL) {
  $query = db_select('taxonomy_term_field_data', 'td');
  $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
  $query->fields('td', array('tid'));
  $query->addField('tn', 'nid', 'node_nid');
  $query->orderby('td.weight');
  $query->orderby('td.name');
  $query->condition('tn.nid', $nids, 'IN');
  $query->addTag('term_access');
  if (!empty($vocabs)) {
    $query->condition('td.vid', $vocabs, 'IN');
  }
  if (!empty($langcode)) {
    $query->condition('td.langcode', $langcode);
  }
 
  $results = array();
  $all_tids = array();
  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 = array();
  foreach ($results as $nid => $tids) {
    foreach ($tids as $tid) {
      $terms[$nid][$tid] = $all_terms[$tid];
    }
  }
  return $terms;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.