NodeTermData.php 5.55 KB
Newer Older
1 2
<?php

3
namespace Drupal\taxonomy\Plugin\views\relationship;
4

5
use Drupal\Core\Database\Database;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\taxonomy\VocabularyStorageInterface;
8
use Drupal\views\ViewExecutable;
9
use Drupal\views\Plugin\views\display\DisplayPluginBase;
10
use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12 13 14 15 16

/**
 * Relationship handler to return the taxonomy terms of nodes.
 *
 * @ingroup views_relationship_handlers
17
 *
18
 * @ViewsRelationship("node_term_data")
19
 */
20
class NodeTermData extends RelationshipPluginBase {
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  /**
   * The vocabulary storage.
   *
   * @var \Drupal\taxonomy\VocabularyStorageInterface
   */
  protected $vocabularyStorage;

  /**
   * Constructs a NodeTermData object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   *   The vocabulary storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->vocabularyStorage = $vocabulary_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
54
      $container->get('entity_type.manager')->getStorage('taxonomy_vocabulary')
55 56 57
    );
  }

58
  /**
59
   * {@inheritdoc}
60 61 62 63 64 65 66 67 68 69 70 71 72 73
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

    // @todo Remove the legacy code.
    // Convert legacy vids option to machine name vocabularies.
    if (!empty($this->options['vids'])) {
      $vocabularies = taxonomy_vocabulary_get_names();
      foreach ($this->options['vids'] as $vid) {
        if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
          $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
        }
      }
    }
74 75
  }

76 77
  protected function defineOptions() {
    $options = parent::defineOptions();
78
    $options['vids'] = ['default' => []];
79 80 81
    return $options;
  }

82
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
83
    $vocabularies = $this->vocabularyStorage->loadMultiple();
84
    $options = [];
85
    foreach ($vocabularies as $voc) {
86
      $options[$voc->id()] = $voc->label();
87 88
    }

89
    $form['vids'] = [
90
      '#type' => 'checkboxes',
91
      '#title' => $this->t('Vocabularies'),
92
      '#options' => $options,
93
      '#default_value' => $this->options['vids'],
94
      '#description' => $this->t('Choose which vocabularies you wish to relate. Remember that every term found will create a new record, so this relationship is best used on just one vocabulary that has only one term per node.'),
95
    ];
96
    parent::buildOptionsForm($form, $form_state);
97 98
  }

99 100 101 102 103 104 105 106 107 108
  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
    // Transform the #type = checkboxes value to a numerically indexed array,
    // because the config schema expects a sequence, not a mapping.
    $vids = $form_state->getValue(['options', 'vids']);
    $form_state->setValue(['options', 'vids'], array_values(array_filter($vids)));
  }

109 110 111
  /**
   * Called to implement a relationship in a query.
   */
112
  public function query() {
113
    $this->ensureMyTable();
114 115

    $def = $this->definition;
116
    $def['table'] = 'taxonomy_term_field_data';
117

118
    if (!array_filter($this->options['vids'])) {
119
      $taxonomy_index = $this->query->addTable('taxonomy_index', $this->relationship);
aspilicious's avatar
aspilicious committed
120
      $def['left_table'] = $taxonomy_index;
121 122 123 124 125 126
      $def['left_field'] = 'tid';
      $def['field'] = 'tid';
      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
    }
    else {
      // If vocabularies are supplied join a subselect instead
127
      $def['left_table'] = $this->tableAlias;
128 129 130
      $def['left_field'] = 'nid';
      $def['field'] = 'nid';
      $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
131
      $def['adjusted'] = TRUE;
132

133
      $query = Database::getConnection()->select('taxonomy_term_field_data', 'td');
134
      $query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
135
      $query->condition('td.vid', array_filter($this->options['vids']), 'IN');
136 137 138
      if (empty($this->query->options['disable_sql_rewrite'])) {
        $query->addTag('taxonomy_term_access');
      }
139
      $query->fields('td');
140
      $query->fields('tn', ['nid']);
141 142 143
      $def['table formula'] = $query;
    }

144
    $join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $def);
145 146 147 148

    // use a short alias for this:
    $alias = $def['table'] . '_' . $this->table;

149
    $this->alias = $this->query->addRelationship($alias, $join, 'taxonomy_term_field_data', $this->relationship);
150
  }
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();

    foreach ($this->options['vids'] as $vocabulary_id) {
      if ($vocabulary = $this->vocabularyStorage->load($vocabulary_id)) {
        $dependencies[$vocabulary->getConfigDependencyKey()][] = $vocabulary->getConfigDependencyName();
      }
    }

    return $dependencies;
  }

167
}