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