Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
39 / 39 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Path | |
100.00% |
39 / 39 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
defineOptions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
buildOptionsForm | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
getArgument | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
getCacheMaxAge | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCacheContexts | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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) { |
46 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
47 | |
48 | $this->currentPath = $current_path; |
49 | } |
50 | |
51 | /** |
52 | * {@inheritdoc} |
53 | */ |
54 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
55 | return new static( |
56 | $configuration, |
57 | $plugin_id, |
58 | $plugin_definition, |
59 | $container->get('path.current') |
60 | ); |
61 | } |
62 | |
63 | /** |
64 | * {@inheritdoc} |
65 | */ |
66 | protected function defineOptions() { |
67 | $options = parent::defineOptions(); |
68 | $options['pop'] = ['default' => 0]; |
69 | $options['route'] = ['default' => FALSE]; |
70 | |
71 | return $options; |
72 | } |
73 | |
74 | /** |
75 | * {@inheritdoc} |
76 | */ |
77 | public function buildOptionsForm(&$form, FormStateInterface $form_state) { |
78 | parent::buildOptionsForm($form, $form_state); |
79 | $form['pop'] = [ |
80 | '#type' => 'textfield', |
81 | '#title' => $this->t('Items to pop off the path'), |
82 | '#description' => $this->t('0 = Current path. 1 = node/1/visitors becomes node/1.'), |
83 | '#default_value' => $this->options['pop'], |
84 | ]; |
85 | $form['route'] = [ |
86 | '#type' => 'checkbox', |
87 | '#title' => $this->t('Use route instead of path'), |
88 | '#description' => $this->t('Convert the path to a route.'), |
89 | '#default_value' => $this->options['route'], |
90 | ]; |
91 | } |
92 | |
93 | /** |
94 | * {@inheritdoc} |
95 | */ |
96 | public function getArgument() { |
97 | if (!$this->currentPath) { |
98 | return ''; |
99 | } |
100 | |
101 | $path = $this->currentPath->getPath(); |
102 | |
103 | $pop = ($this->options['pop']); |
104 | if ($pop > 0) { |
105 | $path = explode('/', $path); |
106 | $path = array_slice($path, 0, -$pop); |
107 | $path = implode('/', $path); |
108 | } |
109 | |
110 | if ($this->options['route']) { |
111 | $route = Url::fromUserInput($path)->getRouteName(); |
112 | return $route; |
113 | } |
114 | |
115 | return $path; |
116 | } |
117 | |
118 | /** |
119 | * {@inheritdoc} |
120 | */ |
121 | public function getCacheMaxAge() { |
122 | return Cache::PERMANENT; |
123 | } |
124 | |
125 | /** |
126 | * {@inheritdoc} |
127 | */ |
128 | public function getCacheContexts() { |
129 | return ['url.path']; |
130 | } |
131 | |
132 | } |