View source
<?php
namespace Drupal\taxonomy\Plugin\views\relationship;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\VocabularyStorageInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeTermData extends RelationshipPluginBase {
protected $vocabularyStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vocabularyStorage = $vocabulary_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.manager')
->getStorage('taxonomy_vocabulary'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
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;
}
}
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['vids'] = array(
'default' => array(),
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$vocabularies = $this->vocabularyStorage
->loadMultiple();
$options = array();
foreach ($vocabularies as $voc) {
$options[$voc
->id()] = $voc
->label();
}
$form['vids'] = array(
'#type' => 'checkboxes',
'#title' => $this
->t('Vocabularies'),
'#options' => $options,
'#default_value' => $this->options['vids'],
'#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.'),
);
parent::buildOptionsForm($form, $form_state);
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$vids = $form_state
->getValue([
'options',
'vids',
]);
$form_state
->setValue([
'options',
'vids',
], array_values(array_filter($vids)));
}
public function query() {
$this
->ensureMyTable();
$def = $this->definition;
$def['table'] = 'taxonomy_term_field_data';
if (!array_filter($this->options['vids'])) {
$taxonomy_index = $this->query
->addTable('taxonomy_index', $this->relationship);
$def['left_table'] = $taxonomy_index;
$def['left_field'] = 'tid';
$def['field'] = 'tid';
$def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
}
else {
$def['left_table'] = $this->tableAlias;
$def['left_field'] = 'nid';
$def['field'] = 'nid';
$def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
$def['adjusted'] = TRUE;
$query = db_select('taxonomy_term_field_data', 'td');
$query
->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
$query
->condition('td.vid', array_filter($this->options['vids']), 'IN');
$query
->addTag('term_access');
$query
->fields('td');
$query
->fields('tn', array(
'nid',
));
$def['table formula'] = $query;
}
$join = \Drupal::service('plugin.manager.views.join')
->createInstance('standard', $def);
$alias = $def['table'] . '_' . $this->table;
$this->alias = $this->query
->addRelationship($alias, $join, 'taxonomy_term_field_data', $this->relationship);
}
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;
}
}