Coverage index

src/Plugin/views/argument_default/Path.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Plugin\views\argument_default;
4
5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheableDependencyInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Path\CurrentPathStack;
9
use Drupal\Core\StringTranslation\TranslatableMarkup;
10
use Drupal\Core\Url;
11
use Drupal\views\Attribute\ViewsArgumentDefault;
12
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Default argument plugin for current or relative path.
17
 *
18
 * @ingroup views_argument_default_plugins
19
 */
20
#[ViewsArgumentDefault(
21
  id: "visitors_path",
22
  title: new TranslatableMarkup("Current Path or Route")
23
)]
24
class Path extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
25
26
  /**
27
   * The current path.
28
   *
29
   * @var \Drupal\Core\Path\CurrentPathStack|null
30
   */
31
  protected $currentPath;
32
33
  /**
34
   * Constructs a new Node instance.
35
   *
36
   * @param array $configuration
37
   *   A configuration array containing information about the plugin instance.
38
   * @param string $plugin_id
39
   *   The plugin_id for the plugin instance.
40
   * @param mixed $plugin_definition
41
   *   The plugin implementation definition.
42
   * @param \Drupal\Core\Path\CurrentPathStack|null $current_path
43
   *   The current_path.
44
   */
45
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ?CurrentPathStack $current_path) {
4612
    parent::__construct($configuration, $plugin_id, $plugin_definition);
47
4812
    $this->currentPath = $current_path;
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
5512
    return new static(
5612
      $configuration,
5712
      $plugin_id,
5812
      $plugin_definition,
5912
      $container->get('path.current')
6012
    );
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  protected function defineOptions() {
6712
    $options = parent::defineOptions();
6812
    $options['pop'] = ['default' => 0];
6912
    $options['route'] = ['default' => FALSE];
70
7112
    return $options;
72
  }
73
74
  /**
75
   * {@inheritdoc}
76
   */
77
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
781
    parent::buildOptionsForm($form, $form_state);
791
    $form['pop'] = [
801
      '#type' => 'textfield',
811
      '#title' => $this->t('Items to pop off the path'),
821
      '#description' => $this->t('0 = Current path. 1 = node/1/visitors becomes node/1.'),
831
      '#default_value' => $this->options['pop'],
841
    ];
851
    $form['route'] = [
861
      '#type' => 'checkbox',
871
      '#title' => $this->t('Use route instead of path'),
881
      '#description' => $this->t('Convert the path to a route.'),
891
      '#default_value' => $this->options['route'],
901
    ];
91
  }
92
93
  /**
94
   * {@inheritdoc}
95
   */
96
  public function getArgument() {
973
    if (!$this->currentPath) {
981
      return '';
99
    }
100
1012
    $path = $this->currentPath->getPath();
102
1032
    $pop = ($this->options['pop']);
1042
    if ($pop > 0) {
1052
      $path = explode('/', $path);
1062
      $path = array_slice($path, 0, -$pop);
1072
      $path = implode('/', $path);
108
    }
109
1102
    if ($this->options['route']) {
1111
      $route = Url::fromUserInput($path)->getRouteName();
1121
      return $route;
113
    }
114
1151
    return $path;
116
  }
117
118
  /**
119
   * {@inheritdoc}
120
   */
121
  public function getCacheMaxAge() {
12212
    return Cache::PERMANENT;
123
  }
124
125
  /**
126
   * {@inheritdoc}
127
   */
128
  public function getCacheContexts() {
12912
    return ['url.path'];
130
  }
131
132
}