Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / argument_default / QueryParameter.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
9 /**
10  * A query parameter argument default handler.
11  *
12  * @ingroup views_argument_default_plugins
13  *
14  * @ViewsArgumentDefault(
15  *   id = "query_parameter",
16  *   title = @Translation("Query parameter")
17  * )
18  */
19 class QueryParameter extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function defineOptions() {
25     $options = parent::defineOptions();
26     $options['query_param'] = ['default' => ''];
27     $options['fallback'] = ['default' => ''];
28     $options['multiple'] = ['default' => 'and'];
29
30     return $options;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
37     parent::buildOptionsForm($form, $form_state);
38     $form['query_param'] = [
39       '#type' => 'textfield',
40       '#title' => $this->t('Query parameter'),
41       '#description' => $this->t('The query parameter to use.'),
42       '#default_value' => $this->options['query_param'],
43     ];
44     $form['fallback'] = [
45       '#type' => 'textfield',
46       '#title' => $this->t('Fallback value'),
47       '#description' => $this->t('The fallback value to use when the above query parameter is not present.'),
48       '#default_value' => $this->options['fallback'],
49     ];
50     $form['multiple'] = [
51       '#type' => 'radios',
52       '#title' => $this->t('Multiple values'),
53       '#description' => $this->t('Conjunction to use when handling multiple values. E.g. "?value[0]=a&value[1]=b".'),
54       '#default_value' => $this->options['multiple'],
55       '#options' => [
56         'and' => $this->t('AND'),
57         'or' => $this->t('OR'),
58       ],
59     ];
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getArgument() {
66     $current_request = $this->view->getRequest();
67
68     if ($current_request->query->has($this->options['query_param'])) {
69       $param = $current_request->query->get($this->options['query_param']);
70       if (is_array($param)) {
71         $conjunction = ($this->options['multiple'] == 'and') ? ',' : '+';
72         $param = implode($conjunction, $param);
73       }
74
75       return $param;
76     }
77     else {
78       // Otherwise, use the fixed fallback value.
79       return $this->options['fallback'];
80     }
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getCacheMaxAge() {
87     return Cache::PERMANENT;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function getCacheContexts() {
94     return ['url'];
95   }
96
97 }