Lines: 64.71%
| Line | Hits | Source |
|---|---|---|
| 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 | ) { | |
| 45 | 3 | 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) { | |
| 52 | 3 | return new static( |
| 53 | 3 | $configuration, |
| 54 | 3 | $plugin_id, |
| 55 | 3 | $plugin_definition, |
| 56 | 3 | new CurrentPathValue($container->get('path.current')), |
| 57 | 3 | $container->get('path.validator'), |
| 58 | 3 | ); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * {@inheritdoc} | |
| 63 | */ | |
| 64 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
| 65 | 0 | parent::buildOptionsForm($form, $form_state); |
| 66 | 0 | $form['pop']['#title'] = $this->t('Path parts to remove'); |
| 67 | 0 | $form['pop']['#description'] = $this->t('How many parts to drop from the end of the page path.'); |
| 68 | 0 | $form['append']['#title'] = $this->t('Path parts to add'); |
| 69 | 0 | $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() { | |
| 76 | 2 | $path = $this->adjustedPath(); |
| 77 | 2 | $url = $path === '' ? NULL : $this->pathValidator->getUrlIfValidWithoutAccessCheck($path); |
| 78 | 2 | $this->applyValue($url ? $url->getRouteName() : ''); |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * {@inheritdoc} | |
| 83 | */ | |
| 84 | protected function summaryLabel(): TranslatableMarkup { | |
| 85 | 0 | return $this->t('Current route'); |
| 86 | } | |
| 87 | ||
| 88 | } |