Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Path
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
7 / 7
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 defineOptions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 buildOptionsForm
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 getArgument
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 getCacheMaxAge
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCacheContexts
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\visitors\Plugin\views\argument_default;
4
5use Drupal\Core\Cache\Cache;
6use Drupal\Core\Cache\CacheableDependencyInterface;
7use Drupal\Core\Form\FormStateInterface;
8use Drupal\Core\Path\CurrentPathStack;
9use Drupal\Core\Url;
10use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
11use Symfony\Component\DependencyInjection\ContainerInterface;
12
13/**
14 * Default argument plugin for current or relative path.
15 *
16 * @ingroup views_argument_default_plugins
17 *
18 * @ViewsArgumentDefault(
19 *   id = "visitors_path",
20 *   title = @Translation("Current Path or Route")
21 * )
22 */
23class Path extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
24
25  /**
26   * The current path.
27   *
28   * @var \Drupal\Core\Path\CurrentPathStack|null
29   */
30  protected $currentPath;
31
32  /**
33   * Constructs a new Node instance.
34   *
35   * @param array $configuration
36   *   A configuration array containing information about the plugin instance.
37   * @param string $plugin_id
38   *   The plugin_id for the plugin instance.
39   * @param mixed $plugin_definition
40   *   The plugin implementation definition.
41   * @param \Drupal\Core\Path\CurrentPathStack|null $current_path
42   *   The current_path.
43   */
44  public function __construct(array $configuration, $plugin_id, $plugin_definition, ?CurrentPathStack $current_path) {
45    parent::__construct($configuration, $plugin_id, $plugin_definition);
46
47    $this->currentPath = $current_path;
48  }
49
50  /**
51   * {@inheritdoc}
52   */
53  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
54    return new static(
55      $configuration,
56      $plugin_id,
57      $plugin_definition,
58      $container->get('path.current')
59    );
60  }
61
62  /**
63   * {@inheritdoc}
64   */
65  protected function defineOptions() {
66    $options = parent::defineOptions();
67    $options['pop'] = ['default' => 0];
68    $options['route'] = ['default' => FALSE];
69
70    return $options;
71  }
72
73  /**
74   * {@inheritdoc}
75   */
76  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
77    parent::buildOptionsForm($form, $form_state);
78    $form['pop'] = [
79      '#type' => 'textfield',
80      '#title' => $this->t('Items to pop off the path'),
81      '#description' => $this->t('0 = Current path. 1 = node/1/visitors becomes node/1.'),
82      '#default_value' => $this->options['pop'],
83    ];
84    $form['route'] = [
85      '#type' => 'checkbox',
86      '#title' => $this->t('Use route instead of path'),
87      '#description' => $this->t('Convert the path to a route.'),
88      '#default_value' => $this->options['route'],
89    ];
90  }
91
92  /**
93   * {@inheritdoc}
94   */
95  public function getArgument() {
96    if (!$this->currentPath) {
97      return '';
98    }
99
100    $path = $this->currentPath->getPath();
101
102    $pop = ($this->options['pop']);
103    if ($pop > 0) {
104      $path = explode('/', $path);
105      $path = array_slice($path, 0, -$pop);
106      $path = implode('/', $path);
107    }
108
109    if ($this->options['route']) {
110      $route = Url::fromUserInput($path)->getRouteName();
111      return $route;
112    }
113
114    return $path;
115  }
116
117  /**
118   * {@inheritdoc}
119   */
120  public function getCacheMaxAge() {
121    return Cache::PERMANENT;
122  }
123
124  /**
125   * {@inheritdoc}
126   */
127  public function getCacheContexts() {
128    return ['url.path'];
129  }
130
131}