3 namespace Drupal\taxonomy\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\Plugin\views\field\PrerenderList;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Drupal\taxonomy\VocabularyStorageInterface;
13 * Field handler to display all taxonomy terms of a node.
15 * @ingroup views_field_handlers
17 * @ViewsField("taxonomy_index_tid")
19 class TaxonomyIndexTid extends PrerenderList {
22 * The vocabulary storage.
24 * @var \Drupal\taxonomy\VocabularyStorageInterface.
26 protected $vocabularyStorage;
29 * Constructs a TaxonomyIndexTid object.
31 * @param array $configuration
32 * A configuration array containing information about the plugin instance.
33 * @param string $plugin_id
34 * The plugin_id for the plugin instance.
35 * @param mixed $plugin_definition
36 * The plugin implementation definition.
37 * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
38 * The vocabulary storage.
40 public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage) {
41 parent::__construct($configuration, $plugin_id, $plugin_definition);
42 $this->vocabularyStorage = $vocabulary_storage;
48 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
53 $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
60 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
61 parent::init($view, $display, $options);
63 // @todo: Wouldn't it be possible to use $this->base_table and no if here?
64 if ($view->storage->get('base_table') == 'node_field_revision') {
65 $this->additional_fields['nid'] = ['table' => 'node_field_revision', 'field' => 'nid'];
68 $this->additional_fields['nid'] = ['table' => 'node_field_data', 'field' => 'nid'];
72 protected function defineOptions() {
73 $options = parent::defineOptions();
75 $options['link_to_taxonomy'] = ['default' => TRUE];
76 $options['limit'] = ['default' => FALSE];
77 $options['vids'] = ['default' => []];
83 * Provide "link to term" option.
85 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
86 $form['link_to_taxonomy'] = [
87 '#title' => $this->t('Link this field to its term page'),
88 '#type' => 'checkbox',
89 '#default_value' => !empty($this->options['link_to_taxonomy']),
93 '#type' => 'checkbox',
94 '#title' => $this->t('Limit terms by vocabulary'),
95 '#default_value' => $this->options['limit'],
99 $vocabularies = $this->vocabularyStorage->loadMultiple();
100 foreach ($vocabularies as $voc) {
101 $options[$voc->id()] = $voc->label();
105 '#type' => 'checkboxes',
106 '#title' => $this->t('Vocabularies'),
107 '#options' => $options,
108 '#default_value' => $this->options['vids'],
111 ':input[name="options[limit]"]' => ['checked' => TRUE],
117 parent::buildOptionsForm($form, $form_state);
121 * Add this term to the query
123 public function query() {
124 $this->addAdditionalFields();
127 public function preRender(&$values) {
128 $vocabularies = $this->vocabularyStorage->loadMultiple();
129 $this->field_alias = $this->aliases['nid'];
131 foreach ($values as $result) {
132 if (!empty($result->{$this->aliases['nid']})) {
133 $nids[] = $result->{$this->aliases['nid']};
138 $vocabs = array_filter($this->options['vids']);
139 if (empty($this->options['limit'])) {
142 $result = \Drupal::entityManager()->getStorage('taxonomy_term')->getNodeTerms($nids, $vocabs);
144 foreach ($result as $node_nid => $data) {
145 foreach ($data as $tid => $term) {
146 $this->items[$node_nid][$tid]['name'] = \Drupal::entityManager()->getTranslationFromContext($term)->label();
147 $this->items[$node_nid][$tid]['tid'] = $tid;
148 $this->items[$node_nid][$tid]['vocabulary_vid'] = $term->bundle();
149 $this->items[$node_nid][$tid]['vocabulary'] = $vocabularies[$term->bundle()]->label();
151 if (!empty($this->options['link_to_taxonomy'])) {
152 $this->items[$node_nid][$tid]['make_link'] = TRUE;
153 $this->items[$node_nid][$tid]['path'] = 'taxonomy/term/' . $tid;
160 public function render_item($count, $item) {
161 return $item['name'];
164 protected function documentSelfTokens(&$tokens) {
165 $tokens['{{ ' . $this->options['id'] . '__tid' . ' }}'] = $this->t('The taxonomy term ID for the term.');
166 $tokens['{{ ' . $this->options['id'] . '__name' . ' }}'] = $this->t('The taxonomy term name for the term.');
167 $tokens['{{ ' . $this->options['id'] . '__vocabulary_vid' . ' }}'] = $this->t('The machine name for the vocabulary the term belongs to.');
168 $tokens['{{ ' . $this->options['id'] . '__vocabulary' . ' }}'] = $this->t('The name for the vocabulary the term belongs to.');
171 protected function addSelfTokens(&$tokens, $item) {
172 foreach (['tid', 'name', 'vocabulary_vid', 'vocabulary'] as $token) {
173 $tokens['{{ ' . $this->options['id'] . '__' . $token . ' }}'] = isset($item[$token]) ? $item[$token] : '';