Coverage index

src/Plugin/views/filter/CurrentRoute.php

Lines: 64.71%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Plugin\views\filter;
6
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Path\PathValidatorInterface;
9
use Drupal\Core\StringTranslation\TranslatableMarkup;
10
use Drupal\views\Attribute\ViewsFilter;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Filters a route column by the route of the current path.
15
 *
16
 * The path is adjusted the same way as the current path filter, then
17
 * converted to a route name.
18
 *
19
 * @ingroup views_filter_handlers
20
 */
21
#[ViewsFilter("visitors_current_route")]
22
class CurrentRoute extends CurrentPathFilterBase {
23
24
  /**
25
   * Constructs a current route filter.
26
   *
27
   * @param array $configuration
28
   *   A configuration array containing information about the plugin instance.
29
   * @param string $plugin_id
30
   *   The plugin ID for the plugin instance.
31
   * @param mixed $plugin_definition
32
   *   The plugin implementation definition.
33
   * @param \Drupal\visitors\Plugin\views\filter\CurrentPathValue $currentPath
34
   *   The current path, with parts removed or added.
35
   * @param \Drupal\Core\Path\PathValidatorInterface $pathValidator
36
   *   The path validator.
37
   */
38
  public function __construct(
39
    array $configuration,
40
    $plugin_id,
41
    $plugin_definition,
42
    CurrentPathValue $currentPath,
43
    protected PathValidatorInterface $pathValidator,
44
  ) {
453
    parent::__construct($configuration, $plugin_id, $plugin_definition, $currentPath);
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
523
    return new static(
533
      $configuration,
543
      $plugin_id,
553
      $plugin_definition,
563
      new CurrentPathValue($container->get('path.current')),
573
      $container->get('path.validator'),
583
    );
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
650
    parent::buildOptionsForm($form, $form_state);
660
    $form['pop']['#title'] = $this->t('Path parts to remove');
670
    $form['pop']['#description'] = $this->t('How many parts to drop from the end of the page path.');
680
    $form['append']['#title'] = $this->t('Path parts to add');
690
    $form['append']['#description'] = $this->t('Parts to append, separated by slashes. Leave this empty to use the remaining path.');
70
  }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
  public function query() {
762
    $path = $this->adjustedPath();
772
    $url  = $path === '' ? NULL : $this->pathValidator->getUrlIfValidWithoutAccessCheck($path);
782
    $this->applyValue($url ? $url->getRouteName() : '');
79
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84
  protected function summaryLabel(): TranslatableMarkup {
850
    return $this->t('Current route');
86
  }
87
88
}