You are here

public function TaxonomyEntityIndexTid::preRender in Taxonomy Entity Index 8

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 TaxonomyIndexTid::preRender

File

src/Plugin/views/field/TaxonomyEntityIndexTid.php, line 75

Class

TaxonomyEntityIndexTid
Field handler to display all taxonomy terms of an entity.

Namespace

Drupal\taxonomy_entity_index\Plugin\views\field

Code

public function preRender(&$values) {
  $this->field_alias = $this->aliases['entity_id'];
  $entity_ids = [];
  foreach ($values as $result) {
    if (!empty($result->{$this->field_alias})) {
      $entity_ids[] = $result->{$this->field_alias};
    }
  }
  if ($entity_ids) {
    $query = $this->database
      ->select('taxonomy_term_data', 'td');
    $query
      ->innerJoin('taxonomy_entity_index', 'tei', 'td.tid = tei.tid');
    $query
      ->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
    $query
      ->fields('td');
    $query
      ->addField('tei', 'entity_id', 'entity_id');
    $query
      ->addField('tei', 'entity_type', 'entity_type');
    $query
      ->addField('tei', 'revision_id', 'revision_id');
    $query
      ->addField('tv', 'name', 'vocabulary');
    $query
      ->addField('tv', 'machine_name', 'vocabulary_machine_name');
    $query
      ->orderby('td.weight');
    $query
      ->orderby('td.name');
    $query
      ->condition('tei.entity_id', $entity_ids);
    $query
      ->condition('tei.entity_type', $this->baseTableInfo['table']['entity type']);
    $query
      ->addTag('term_access');
    $vocabs = array_filter($this->options['vids']);
    if (!empty($this->options['limit']) && !empty($vocabs)) {
      $query
        ->condition('tv.machine_name', $vocabs);
    }
    $result = $query
      ->execute();
    foreach ($result as $term) {
      $this->items[$term->entity_id][$term->tid]['name'] = Html::escape($term->name);
      $this->items[$term->entity_id][$term->tid]['tid'] = $term->tid;
      $this->items[$term->entity_id][$term->tid]['vocabulary_machine_name'] = Html::escape($term->vocabulary_machine_name);
      $this->items[$term->entity_id][$term->tid]['vocabulary'] = Html::escape($term->vocabulary);
      if (!empty($this->options['link_to_taxonomy'])) {
        $this->items[$term->entity_id][$term->tid]['make_link'] = TRUE;
        $this->items[$term->entity_id][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
      }
    }
  }
}