diff --git a/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php b/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php new file mode 100644 index 00000000000..d8eb1ab3f9b --- /dev/null +++ b/core/modules/views/src/Plugin/views/argument_default/QueryParameter.php @@ -0,0 +1,84 @@ + ''); + $options['fallback'] = array('default' => ''); + $options['multiple'] = array('default' => 'and'); + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + $form['query_param'] = array( + '#type' => 'textfield', + '#title' => $this->t('Query parameter'), + '#description' => $this->t('The query parameter to use.'), + '#default_value' => $this->options['query_param'], + ); + $form['fallback'] = array( + '#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'] = array( + '#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' => array( + 'and' => $this->t('AND'), + 'or' => $this->t('OR'), + ), + ); + } + + /** + * {@inheritdoc} + */ + public function getArgument() { + $current_request = $this->view->getRequest(); + + if ($current_request->query->has($this->options['query_param'])) { + $param = $current_request->query->get($this->options['query_param']); + 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']; + } + } + +} diff --git a/core/modules/views/tests/src/Plugin/argument_default/QueryParameterTest.php b/core/modules/views/tests/src/Plugin/argument_default/QueryParameterTest.php new file mode 100644 index 00000000000..75295fe81c5 --- /dev/null +++ b/core/modules/views/tests/src/Plugin/argument_default/QueryParameterTest.php @@ -0,0 +1,100 @@ + 'Tests \Drupal\views\Plugin\views\argument_default\QueryParameter', + 'description' => '', + 'group' => 'Views Plugin', + ); + } + + /** + * Test the getArgument() method. + * + * @covers ::getArgument() + * @dataProvider providerGetArgument + */ + public function testGetArgument($options, Request $request, $expected) { + $view = $this->getMockBuilder('Drupal\views\ViewExecutable') + ->disableOriginalConstructor() + ->setMethods(NULL) + ->getMock(); + $view->setRequest($request); + $display_plugin = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase') + ->disableOriginalConstructor() + ->getMock(); + + $raw = new QueryParameter(array(), 'query_parameter', array()); + $raw->init($view, $display_plugin, $options); + $this->assertEquals($expected, $raw->getArgument()); + } + + /** + * Provides data for testGetArgument(). + * + * @return array + * An array of test data, with the following entries: + * - first entry: the options for the plugin. + * - second entry: the request object to test with. + * - third entry: the expected default argument value. + */ + public function providerGetArgument() { + $data = array(); + + $single[] = array( + 'query_param' => 'test', + ); + $single[] = new Request(array('test' => 'data')); + $single[] = 'data'; + $data[] = $single; + + $single[] = array( + 'query_param' => 'test', + 'multiple' => 'AND' + ); + $single[] = new Request(array('test' => array('data1', 'data2'))); + $single[] = 'data1+data2'; + $data[] = $single; + + $single[] = array( + 'query_param' => 'test', + 'multiple' => 'OR' + ); + $single[] = new Request(array('test' => array('data1', 'data2'))); + $single[] = 'data1,data2'; + $data[] = $single; + + $single[] = array( + 'query_param' => 'test', + 'fallback' => 'blub', + ); + $single[] = new Request(array()); + $single[] = 'blub'; + $data[] = $single; + + return $data; + } + +} +