function TaxonomyIndexTid::preRender

Same name in other branches
  1. 9 core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid::preRender()
  2. 10 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) {
        $vocabs = array_filter($this->options['vids']);
        if (empty($this->options['limit'])) {
            $vocabs = [];
        }
        $result = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
            ->getNodeTerms($nids, $vocabs);
        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.