3 namespace Drupal\taxonomy\Plugin\views\argument;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Argument handler for taxonomy terms with depth.
14 * This handler is actually part of the node table and has some restrictions,
15 * because it uses a subquery to find nodes with.
17 * @ingroup views_argument_handlers
19 * @ViewsArgument("taxonomy_index_tid_depth")
21 class IndexTidDepth extends ArgumentPluginBase implements ContainerFactoryPluginInterface {
24 * @var EntityStorageInterface
26 protected $termStorage;
31 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $termStorage) {
32 parent::__construct($configuration, $plugin_id, $plugin_definition);
34 $this->termStorage = $termStorage;
40 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41 return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorage('taxonomy_term'));
44 protected function defineOptions() {
45 $options = parent::defineOptions();
47 $options['depth'] = ['default' => 0];
48 $options['break_phrase'] = ['default' => FALSE];
49 $options['use_taxonomy_term_path'] = ['default' => FALSE];
54 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
57 '#title' => $this->t('Depth'),
58 '#default_value' => $this->options['depth'],
59 '#description' => $this->t('The depth will match nodes tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get nodes that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).'),
62 $form['break_phrase'] = [
63 '#type' => 'checkbox',
64 '#title' => $this->t('Allow multiple values'),
65 '#description' => $this->t('If selected, users can enter multiple values in the form of 1+2+3. Due to the number of JOINs it would require, AND will be treated as OR with this filter.'),
66 '#default_value' => !empty($this->options['break_phrase']),
69 parent::buildOptionsForm($form, $form_state);
73 * Override defaultActions() to remove summary actions.
75 protected function defaultActions($which = NULL) {
77 if (in_array($which, ['ignore', 'not found', 'empty', 'default'])) {
78 return parent::defaultActions($which);
82 $actions = parent::defaultActions();
83 unset($actions['summary asc']);
84 unset($actions['summary desc']);
85 unset($actions['summary asc by count']);
86 unset($actions['summary desc by count']);
90 public function query($group_by = FALSE) {
91 $this->ensureMyTable();
93 if (!empty($this->options['break_phrase'])) {
94 $break = static::breakString($this->argument);
95 if ($break->value === [-1]) {
99 $operator = (count($break->value) > 1) ? 'IN' : '=';
100 $tids = $break->value;
104 $tids = $this->argument;
106 // Now build the subqueries.
107 $subquery = db_select('taxonomy_index', 'tn');
108 $subquery->addField('tn', 'nid');
109 $where = db_or()->condition('tn.tid', $tids, $operator);
112 if ($this->options['depth'] > 0) {
113 $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
115 foreach (range(1, abs($this->options['depth'])) as $count) {
116 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
117 $where->condition("th$count.tid", $tids, $operator);
121 elseif ($this->options['depth'] < 0) {
122 foreach (range(1, abs($this->options['depth'])) as $count) {
123 $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
124 $where->condition("th$count.tid", $tids, $operator);
129 $subquery->condition($where);
130 $this->query->addWhere(0, "$this->tableAlias.$this->realField", $subquery, 'IN');
133 public function title() {
134 $term = $this->termStorage->load($this->argument);
136 return $term->getName();
139 return $this->t('No name');