class QueryParameter
Same name and namespace in other branches
- 11.x core/modules/views/src/Plugin/views/argument_default/QueryParameter.php \Drupal\views\Plugin\views\argument_default\QueryParameter
A query parameter argument default handler.
Plugin annotation
@ViewsArgumentDefault(
id = "query_parameter",
title = @Translation("Query parameter")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
- class \Drupal\views\Plugin\views\PluginBase extends \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase implements \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\argument_default\QueryParameter extends \Drupal\Core\Cache\CacheableDependencyInterface implements \Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase
- class \Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase implements \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\PluginBase extends \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of QueryParameter
Related topics
1 file declares its use of QueryParameter
- QueryParameterTest.php in core/
modules/ views/ tests/ src/ Unit/ Plugin/ argument_default/ QueryParameterTest.php
File
-
core/
modules/ views/ src/ Plugin/ views/ argument_default/ QueryParameter.php, line 20
Namespace
Drupal\views\Plugin\views\argument_defaultView source
class QueryParameter extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['query_param'] = [
'default' => '',
];
$options['fallback'] = [
'default' => '',
];
$options['multiple'] = [
'default' => 'and',
];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['query_param'] = [
'#type' => 'textfield',
'#title' => $this->t('Query parameter'),
'#description' => $this->t('The query parameter to use.'),
'#default_value' => $this->options['query_param'],
];
$form['fallback'] = [
'#type' => 'textfield',
'#title' => $this->t('Fallback value'),
'#description' => $this->t('The fallback value to use when the above query parameter is not present.'),
'#default_value' => $this->options['fallback'],
];
$form['multiple'] = [
'#type' => 'radios',
'#title' => $this->t('Multiple values'),
'#description' => $this->t('Conjunction to use when handling multiple values. E.g. "?value[0]=a&value[1]=b".'),
'#default_value' => $this->options['multiple'],
'#options' => [
'and' => $this->t('AND'),
'or' => $this->t('OR'),
],
];
}
/**
* {@inheritdoc}
*/
public function getArgument() {
$current_request = $this->view
->getRequest();
// Convert a[b][c][d] into ['a', 'b', 'c', 'd'].
$path = array_filter(preg_split('#(\\[|\\]\\[|\\])#', $this->options['query_param']));
if ($current_request->query
->has($path[0])) {
$query = $current_request->query
->all();
$param = NestedArray::getValue($query, $path);
if (is_array($param)) {
$conjunction = $this->options['multiple'] == 'and' ? ',' : '+';
$param = implode($conjunction, $param);
}
return $param;
}
else {
// Otherwise, use the fixed fallback value.
return $this->options['fallback'];
}
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return [
'url',
];
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.