You are here

public function TaxonomyIndexTid::preRender in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid::preRender()

Runs before any fields are rendered.

This gives the handlers some time to set up before any handler has been rendered.

Parameters

\Drupal\views\ResultRow[] $values: An array of all ResultRow objects returned from the query.

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;
        }
      }
    }
  }
}