SearchApiAllTerms.php in Search API 8
File
src/Plugin/views/argument/SearchApiAllTerms.php
View source
<?php
namespace Drupal\search_api\Plugin\views\argument;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SearchApiAllTerms extends SearchApiTerm {
protected $entityTypeManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin
->setEntityTypeManager($container
->get('entity_type.manager'));
return $plugin;
}
public function getEntityTypeManager() : EntityTypeManagerInterface {
return $this->entityTypeManager ?: \Drupal::entityTypeManager();
}
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) : self {
$this->entityTypeManager = $entity_type_manager;
return $this;
}
public function query($group_by = FALSE) {
if (empty($this->value)) {
$this
->fillValue();
if (empty($this->value)) {
return;
}
}
$not_negated = empty($this->options['not']);
$conjunction = strtoupper($this->operator);
$use_in_conditions = $conjunction === 'OR';
if ($not_negated) {
$operator = $use_in_conditions ? 'IN' : '=';
}
else {
if ($use_in_conditions) {
$conjunction = 'AND';
$operator = 'NOT IN';
}
else {
$conjunction = 'OR';
$operator = '<>';
}
}
try {
$terms = $this
->getEntityTypeManager()
->getStorage('taxonomy_term')
->loadMultiple($this->value);
} catch (PluginException $e) {
$this->query
->abort($this
->t('Could not load taxonomy terms.'));
return;
}
if (empty($terms)) {
if ($not_negated) {
$this->query
->abort($this
->t('No valid taxonomy term IDs given for "All taxonomy term fields" contextual filter.'));
}
return;
}
if ($not_negated && $conjunction == 'AND' && count($terms) < count($this->value)) {
$this->query
->abort($this
->t('Invalid taxonomy term ID given for "All taxonomy term fields" contextual filter.'));
return;
}
$vocabulary_fields = $this->definition['vocabulary_fields'];
$vocabulary_fields += [
'' => [],
];
$values = $multi_field_values = [];
$term_conditions = $this->query
->createConditionGroup($conjunction);
$this->query
->addConditionGroup($term_conditions);
foreach ($terms as $term) {
$vocabulary_id = $term
->bundle();
$term_id = $term
->id();
$fields_count = count($vocabulary_fields[$vocabulary_id] ?? []) + count($vocabulary_fields['']);
if ($conjunction === 'AND' && $fields_count === 0) {
$variables = [
'@id' => $term_id,
'%label' => $term
->label(),
'%vocabulary' => $vocabulary_id,
];
$this->query
->abort($this
->t('"All taxonomy term fields" contextual filter could not be applied as taxonomy term %label (ID: @id) belongs to vocabulary %vocabulary, not contained in any indexed fields.', $variables));
return;
}
if ($this->operator === 'and' && $fields_count > 1) {
foreach ($vocabulary_fields[$vocabulary_id] ?? [] as $field) {
$multi_field_values[$term_id][] = $field;
}
foreach ($vocabulary_fields[''] as $field) {
$multi_field_values[$term_id][] = $field;
}
}
else {
foreach ($vocabulary_fields[$vocabulary_id] ?? [] as $field) {
$values[$field][] = $term_id;
}
foreach ($vocabulary_fields[''] as $field) {
$values[$field][] = $term_id;
}
}
}
foreach ($values as $field => $items) {
if ($use_in_conditions) {
$term_conditions
->addCondition($field, $items, $operator);
}
else {
foreach ($items as $value) {
$term_conditions
->addCondition($field, $value, $operator);
}
}
}
foreach ($multi_field_values as $value => $fields) {
$flipped_conjunction = $conjunction === 'AND' ? 'OR' : 'AND';
$group = $this->query
->createConditionGroup($flipped_conjunction);
$term_conditions
->addConditionGroup($group);
foreach ($fields as $field) {
$group
->addCondition($field, $value, $not_negated ? '=' : '<>');
}
}
if (!$term_conditions
->getConditions()) {
$labels = [];
foreach ($terms as $term) {
$labels[] = $term
->label();
}
$variables = [
'@terms' => implode(', ', $labels),
];
$this->query
->abort($this
->t('"All taxonomy term fields" contextual filter could not be applied as no indexed fields were found matching the given vocabularies of the given terms (@terms).', $variables));
}
}
}
Classes
Name |
Description |
SearchApiAllTerms |
Defines a contextual filter searching through all indexed taxonomy fields. |