3 namespace Drupal\views\Plugin\views\argument_default;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * A query parameter argument default handler.
12 * @ingroup views_argument_default_plugins
14 * @ViewsArgumentDefault(
15 * id = "query_parameter",
16 * title = @Translation("Query parameter")
19 class QueryParameter extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
24 protected function defineOptions() {
25 $options = parent::defineOptions();
26 $options['query_param'] = ['default' => ''];
27 $options['fallback'] = ['default' => ''];
28 $options['multiple'] = ['default' => 'and'];
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'],
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'],
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'],
56 'and' => $this->t('AND'),
57 'or' => $this->t('OR'),
65 public function getArgument() {
66 $current_request = $this->view->getRequest();
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);
78 // Otherwise, use the fixed fallback value.
79 return $this->options['fallback'];
86 public function getCacheMaxAge() {
87 return Cache::PERMANENT;
93 public function getCacheContexts() {