function TaxonomyIndexTid::preRender

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid::preRender()
  2. 8.9.x core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid::preRender()
  3. 11.x core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid::preRender()

Overrides FieldPluginBase::preRender

File

core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php, line 127

Class

TaxonomyIndexTid
Field handler to display all taxonomy terms of a node.

Namespace

Drupal\taxonomy\Plugin\views\field

Code

public function preRender(&$values) {
  $vocabularies = $this->vocabularyStorage
    ->loadMultiple();
  $this->field_alias = $this->aliases['nid'];
  $nids = [];
  foreach ($values as $result) {
    if (!empty($result->{$this->aliases['nid']})) {
      $nids[] = $result->{$this->aliases['nid']};
    }
  }
  if ($nids) {
    $vids = array_filter($this->options['vids']);
    if (empty($this->options['limit'])) {
      $vids = [];
    }
    $result = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
      ->getNodeTerms($nids, $vids);
    foreach ($result as $node_nid => $data) {
      foreach ($data as $tid => $term) {
        $this->items[$node_nid][$tid]['name'] = \Drupal::service('entity.repository')->getTranslationFromContext($term)
          ->label();
        $this->items[$node_nid][$tid]['tid'] = $tid;
        $this->items[$node_nid][$tid]['vocabulary_vid'] = $term->bundle();
        $this->items[$node_nid][$tid]['vocabulary'] = $vocabularies[$term->bundle()]
          ->label();
        if (!empty($this->options['link_to_taxonomy'])) {
          $this->items[$node_nid][$tid]['make_link'] = TRUE;
          $this->items[$node_nid][$tid]['path'] = 'taxonomy/term/' . $tid;
        }
      }
    }
  }
}

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