Lines: 40.68%
| Line | Hits | Source |
|---|---|---|
| 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 | ) { | |
| 37 | 6 | 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) { | |
| 44 | 4 | return new static( |
| 45 | 4 | $configuration, |
| 46 | 4 | $plugin_id, |
| 47 | 4 | $plugin_definition, |
| 48 | 4 | new CurrentPathValue($container->get('path.current')), |
| 49 | 4 | ); |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * {@inheritdoc} | |
| 54 | */ | |
| 55 | protected function defineOptions() { | |
| 56 | 5 | $options = parent::defineOptions(); |
| 57 | 5 | $options['pop'] = ['default' => 0]; |
| 58 | 5 | $options['append'] = ['default' => '']; |
| 59 | ||
| 60 | 5 | return $options; |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * {@inheritdoc} | |
| 65 | */ | |
| 66 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 67 | 0 | parent::buildOptionsForm($form, $form_state); |
| 68 | 0 | unset( |
| 69 | 0 | $form['operator'], |
| 70 | 0 | $form['value'], |
| 71 | 0 | $form['clear_markup_start'], |
| 72 | 0 | $form['clear_markup_end'], |
| 73 | 0 | ); |
| 74 | ||
| 75 | 0 | $form['pop'] = [ |
| 76 | 0 | '#type' => 'number', |
| 77 | 0 | '#title' => $this->t('Items to remove'), |
| 78 | 0 | '#description' => $this->t('0 = current path. 1 turns /node/1/visitors into /node/1.'), |
| 79 | 0 | '#default_value' => $this->options['pop'], |
| 80 | 0 | '#min' => 0, |
| 81 | 0 | '#step' => 1, |
| 82 | 0 | ]; |
| 83 | 0 | $form['append'] = [ |
| 84 | 0 | '#type' => 'textfield', |
| 85 | 0 | '#title' => $this->t('Items to add'), |
| 86 | 0 | '#description' => $this->t('Added after the path. "edit" turns /node/1 into /node/1/edit.'), |
| 87 | 0 | '#default_value' => $this->options['append'], |
| 88 | 0 | ]; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * {@inheritdoc} | |
| 93 | */ | |
| 94 | public function canExpose() { | |
| 95 | 6 | return FALSE; |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * {@inheritdoc} | |
| 100 | */ | |
| 101 | public function adminSummary() { | |
| 102 | 0 | $parts = [(string) $this->summaryLabel()]; |
| 103 | 0 | $pop = (int) $this->options['pop']; |
| 104 | 0 | if ($pop > 0) { |
| 105 | 0 | $parts[] = (string) $this->formatPlural( |
| 106 | 0 | $pop, |
| 107 | 0 | 'remove 1 part', |
| 108 | 0 | 'remove @count parts', |
| 109 | 0 | ); |
| 110 | } | |
| 111 | 0 | $append = trim((string) $this->options['append'], '/'); |
| 112 | 0 | if ($append !== '') { |
| 113 | 0 | $parts[] = (string) $this->t('add @parts', ['@parts' => $append]); |
| 114 | } | |
| 115 | 0 | return implode(', ', $parts); |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * {@inheritdoc} | |
| 120 | */ | |
| 121 | public function getCacheContexts() { | |
| 122 | 6 | return Cache::mergeContexts(parent::getCacheContexts(), ['url.path']); |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * Label used in the views administrative summary. | |
| 127 | */ | |
| 128 | 0 | abstract protected function summaryLabel(): TranslatableMarkup; |
| 129 | ||
| 130 | /** | |
| 131 | * Returns the path these filters compare against. | |
| 132 | */ | |
| 133 | protected function adjustedPath(): string { | |
| 134 | 5 | return $this->currentPath->path( |
| 135 | 5 | (int) $this->options['pop'], |
| 136 | 5 | (string) $this->options['append'], |
| 137 | 5 | ); |
| 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 { | |
| 147 | 5 | $this->ensureMyTable(); |
| 148 | 5 | if (!$this->query instanceof Sql) { |
| 149 | 0 | return; |
| 150 | } | |
| 151 | 5 | if ($value === '') { |
| 152 | 2 | $this->query->addWhereExpression($this->options['group'], '1 = 0'); |
| 153 | 2 | return; |
| 154 | } | |
| 155 | 3 | $field = "$this->tableAlias.$this->realField"; |
| 156 | 3 | $this->query->addWhere($this->options['group'], $field, $value, '='); |
| 157 | } | |
| 158 | ||
| 159 | } |