TaxonomyIndexTid.php 4.87 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid.
6 7
 */

8
namespace Drupal\taxonomy\Plugin\views\field;
9

10
use Drupal\views\ViewExecutable;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\Plugin\views\field\PrerenderList;
13
use Drupal\Component\Annotation\PluginID;
14

15 16 17 18
/**
 * Field handler to display all taxonomy terms of a node.
 *
 * @ingroup views_field_handlers
19
 *
20
 * @PluginID("taxonomy_index_tid")
21 22
 */
class TaxonomyIndexTid extends PrerenderList {
23

24 25 26 27 28 29
  /**
   * Overrides \Drupal\views\Plugin\views\field\PrerenderList::init().
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

30
    // @todo: Wouldn't it be possible to use $this->base_table and no if here?
31 32
    if ($view->storage->get('base_table') == 'node_field_revision') {
      $this->additional_fields['nid'] = array('table' => 'node_field_revision', 'field' => 'nid');
33 34 35 36 37 38
    }
    else {
      $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
    }
  }

39 40
  protected function defineOptions() {
    $options = parent::defineOptions();
41 42 43

    $options['link_to_taxonomy'] = array('default' => TRUE, 'bool' => TRUE);
    $options['limit'] = array('default' => FALSE, 'bool' => TRUE);
44
    $options['vids'] = array('default' => array());
45 46 47 48 49 50 51

    return $options;
  }

  /**
   * Provide "link to term" option.
   */
52
  public function buildOptionsForm(&$form, &$form_state) {
53 54 55 56 57 58 59 60 61
    $form['link_to_taxonomy'] = array(
      '#title' => t('Link this field to its term page'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['link_to_taxonomy']),
    );

    $form['limit'] = array(
      '#type' => 'checkbox',
      '#title' => t('Limit terms by vocabulary'),
dawehner's avatar
dawehner committed
62
      '#default_value' => $this->options['limit'],
63 64 65
    );

    $options = array();
66
    $vocabularies = entity_load_multiple('taxonomy_vocabulary');
67
    foreach ($vocabularies as $voc) {
68
      $options[$voc->id()] = $voc->label();
69 70
    }

71
    $form['vids'] = array(
72 73 74
      '#type' => 'checkboxes',
      '#title' => t('Vocabularies'),
      '#options' => $options,
75
      '#default_value' => $this->options['vids'],
76 77 78 79 80 81 82 83
      '#states' => array(
        'visible' => array(
          ':input[name="options[limit]"]' => array('checked' => TRUE),
        ),
      ),

    );

84
    parent::buildOptionsForm($form, $form_state);
85 86 87 88 89
  }

  /**
   * Add this term to the query
   */
90
  public function query() {
91
    $this->addAdditionalFields();
92 93 94
  }

  function pre_render(&$values) {
95
    $vocabularies = entity_load_multiple('taxonomy_vocabulary');
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    $this->field_alias = $this->aliases['nid'];
    $nids = array();
    foreach ($values as $result) {
      if (!empty($result->{$this->aliases['nid']})) {
        $nids[] = $result->{$this->aliases['nid']};
      }
    }

    if ($nids) {
      $query = db_select('taxonomy_term_data', 'td');
      $query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
      $query->fields('td');
      $query->addField('tn', 'nid', 'node_nid');
      $query->orderby('td.weight');
      $query->orderby('td.name');
      $query->condition('tn.nid', $nids);
      $query->addTag('term_access');
113
      $vocabs = array_filter($this->options['vids']);
114
      if (!empty($this->options['limit']) && !empty($vocabs)) {
115
        $query->condition('td.vid', $vocabs);
116 117 118 119 120 121
      }
      $result = $query->execute();

      foreach ($result as $term) {
        $this->items[$term->node_nid][$term->tid]['name'] = check_plain($term->name);
        $this->items[$term->node_nid][$term->tid]['tid'] = $term->tid;
122 123
        $this->items[$term->node_nid][$term->tid]['vocabulary_vid'] = $term->vid;
        $this->items[$term->node_nid][$term->tid]['vocabulary'] = check_plain($vocabularies[$term->vid]->label());
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

        if (!empty($this->options['link_to_taxonomy'])) {
          $this->items[$term->node_nid][$term->tid]['make_link'] = TRUE;
          $this->items[$term->node_nid][$term->tid]['path'] = 'taxonomy/term/' . $term->tid;
        }
      }
    }
  }

  function render_item($count, $item) {
    return $item['name'];
  }

  function document_self_tokens(&$tokens) {
    $tokens['[' . $this->options['id'] . '-tid' . ']'] = t('The taxonomy term ID for the term.');
    $tokens['[' . $this->options['id'] . '-name' . ']'] = t('The taxonomy term name for the term.');
140
    $tokens['[' . $this->options['id'] . '-vocabulary-vid' . ']'] = t('The machine name for the vocabulary the term belongs to.');
141 142 143
    $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = t('The name for the vocabulary the term belongs to.');
  }

144
  protected function addSelfTokens(&$tokens, $item) {
145 146
    foreach (array('tid', 'name', 'vocabulary_vid', 'vocabulary') as $token) {
      // Replace _ with - for the vocabulary vid.
dawehner's avatar
dawehner committed
147
      $tokens['[' . $this->options['id'] . '-' . str_replace('_', '-', $token) . ']'] = isset($item[$token]) ? $item[$token] : '';
148 149
    }
  }
150

151
}