Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Plugin / views / argument_default / Raw.php
1 <?php
2
3 namespace Drupal\views\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\AliasManagerInterface;
9 use Drupal\Core\Path\CurrentPathStack;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Default argument plugin to use the raw value from the URL.
14  *
15  * @ingroup views_argument_default_plugins
16  *
17  * @ViewsArgumentDefault(
18  *   id = "raw",
19  *   title = @Translation("Raw value from URL")
20  * )
21  */
22 class Raw extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
23
24   /**
25    * The alias manager.
26    *
27    * @var \Drupal\Core\Path\AliasManagerInterface
28    */
29   protected $aliasManager;
30
31   /**
32    * The current path.
33    *
34    * @var \Drupal\Core\Path\CurrentPathStack
35    */
36   protected $currentPath;
37
38   /**
39    * Constructs a Raw object.
40    *
41    * @param array $configuration
42    *   A configuration array containing information about the plugin instance.
43    * @param string $plugin_id
44    *   The plugin_id for the plugin instance.
45    * @param mixed $plugin_definition
46    *   The plugin implementation definition.
47    * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
48    *   The alias manager.
49    * @param \Drupal\Core\Path\CurrentPathStack $current_path
50    *   The current path.
51    */
52   public function __construct(array $configuration, $plugin_id, $plugin_definition, AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
53     parent::__construct($configuration, $plugin_id, $plugin_definition);
54
55     $this->aliasManager = $alias_manager;
56     $this->currentPath = $current_path;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63     return new static(
64       $configuration,
65       $plugin_id,
66       $plugin_definition,
67       $container->get('path.alias_manager'),
68       $container->get('path.current')
69     );
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function defineOptions() {
76     $options = parent::defineOptions();
77     $options['index'] = ['default' => ''];
78     $options['use_alias'] = ['default' => FALSE];
79
80     return $options;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
87     parent::buildOptionsForm($form, $form_state);
88     $form['index'] = [
89       '#type' => 'select',
90       '#title' => $this->t('Path component'),
91       '#default_value' => $this->options['index'],
92       // range(1, 10) returns an array with:
93       // - keys that count from 0 to match PHP array keys from explode().
94       // - values that count from 1 for display to humans.
95       '#options' => range(1, 10),
96       '#description' => $this->t('The numbering starts from 1, e.g. on the page admin/structure/types, the 3rd path component is "types".'),
97     ];
98     $form['use_alias'] = [
99       '#type' => 'checkbox',
100       '#title' => $this->t('Use path alias'),
101       '#default_value' => $this->options['use_alias'],
102       '#description' => $this->t('Use path alias instead of internal path.'),
103     ];
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getArgument() {
110     // Don't trim the leading slash since getAliasByPath() requires it.
111     $path = rtrim($this->currentPath->getPath($this->view->getRequest()), '/');
112     if ($this->options['use_alias']) {
113       $path = $this->aliasManager->getAliasByPath($path);
114     }
115     $args = explode('/', $path);
116     // Drop the empty first element created by the leading slash since the path
117     // component index doesn't take it into account.
118     array_shift($args);
119     if (isset($args[$this->options['index']])) {
120       return $args[$this->options['index']];
121     }
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function getCacheMaxAge() {
128     return Cache::PERMANENT;
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public function getCacheContexts() {
135     return ['url'];
136   }
137
138 }