Coverage index

src/Plugin/views/filter/CurrentPathFilterBase.php

Lines: 40.68%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Plugin\views\filter;
6
7
use Drupal\Core\Cache\Cache;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\StringTranslation\TranslatableMarkup;
10
use Drupal\views\Plugin\views\filter\FilterPluginBase;
11
use Drupal\views\Plugin\views\query\Sql;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Shared options for filters that start from the current path.
16
 */
17
abstract class CurrentPathFilterBase extends FilterPluginBase {
18
19
  /**
20
   * Constructs a current path filter.
21
   *
22
   * @param array $configuration
23
   *   A configuration array containing information about the plugin instance.
24
   * @param string $plugin_id
25
   *   The plugin ID for the plugin instance.
26
   * @param mixed $plugin_definition
27
   *   The plugin implementation definition.
28
   * @param \Drupal\visitors\Plugin\views\filter\CurrentPathValue $currentPath
29
   *   The current path, with parts removed or added.
30
   */
31
  public function __construct(
32
    array $configuration,
33
    $plugin_id,
34
    $plugin_definition,
35
    protected CurrentPathValue $currentPath,
36
  ) {
376
    parent::__construct($configuration, $plugin_id, $plugin_definition);
38
  }
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
444
    return new static(
454
      $configuration,
464
      $plugin_id,
474
      $plugin_definition,
484
      new CurrentPathValue($container->get('path.current')),
494
    );
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  protected function defineOptions() {
565
    $options           = parent::defineOptions();
575
    $options['pop']    = ['default' => 0];
585
    $options['append'] = ['default' => ''];
59
605
    return $options;
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
670
    parent::buildOptionsForm($form, $form_state);
680
    unset(
690
      $form['operator'],
700
      $form['value'],
710
      $form['clear_markup_start'],
720
      $form['clear_markup_end'],
730
    );
74
750
    $form['pop'] = [
760
      '#type'          => 'number',
770
      '#title'         => $this->t('Items to remove'),
780
      '#description'   => $this->t('0 = current path. 1 turns /node/1/visitors into /node/1.'),
790
      '#default_value' => $this->options['pop'],
800
      '#min'           => 0,
810
      '#step'          => 1,
820
    ];
830
    $form['append'] = [
840
      '#type'          => 'textfield',
850
      '#title'         => $this->t('Items to add'),
860
      '#description'   => $this->t('Added after the path. "edit" turns /node/1 into /node/1/edit.'),
870
      '#default_value' => $this->options['append'],
880
    ];
89
  }
90
91
  /**
92
   * {@inheritdoc}
93
   */
94
  public function canExpose() {
956
    return FALSE;
96
  }
97
98
  /**
99
   * {@inheritdoc}
100
   */
101
  public function adminSummary() {
1020
    $parts = [(string) $this->summaryLabel()];
1030
    $pop   = (int) $this->options['pop'];
1040
    if ($pop > 0) {
1050
      $parts[] = (string) $this->formatPlural(
1060
        $pop,
1070
        'remove 1 part',
1080
        'remove @count parts',
1090
      );
110
    }
1110
    $append = trim((string) $this->options['append'], '/');
1120
    if ($append !== '') {
1130
      $parts[] = (string) $this->t('add @parts', ['@parts' => $append]);
114
    }
1150
    return implode(', ', $parts);
116
  }
117
118
  /**
119
   * {@inheritdoc}
120
   */
121
  public function getCacheContexts() {
1226
    return Cache::mergeContexts(parent::getCacheContexts(), ['url.path']);
123
  }
124
125
  /**
126
   * Label used in the views administrative summary.
127
   */
1280
  abstract protected function summaryLabel(): TranslatableMarkup;
129
130
  /**
131
   * Returns the path these filters compare against.
132
   */
133
  protected function adjustedPath(): string {
1345
    return $this->currentPath->path(
1355
      (int) $this->options['pop'],
1365
      (string) $this->options['append'],
1375
    );
138
  }
139
140
  /**
141
   * Compares this field to a value, or matches nothing.
142
   *
143
   * @param string $value
144
   *   The path or route. An empty value matches no rows.
145
   */
146
  protected function applyValue(string $value): void {
1475
    $this->ensureMyTable();
1485
    if (!$this->query instanceof Sql) {
1490
      return;
150
    }
1515
    if ($value === '') {
1522
      $this->query->addWhereExpression($this->options['group'], '1 = 0');
1532
      return;
154
    }
1553
    $field = "$this->tableAlias.$this->realField";
1563
    $this->query->addWhere($this->options['group'], $field, $value, '=');
157
  }
158
159
}