Issue #2255659 by dawehner, xjm, jrbeeman, damiankloip: Added Provide a GET query parameter default argument plugin.

8.0.x
Alex Pott 2014-06-11 12:06:48 +01:00
parent 32ea6a30b8
commit 742fe6182b
2 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,84 @@
<?php
/**
* @file
* Contains \Drupal\views\Plugin\views\argument_default\QueryParameter.
*/
namespace Drupal\views\Plugin\views\argument_default;
/**
* A query parameter argument default handler.
*
* @ingroup views_argument_default_plugins
*
* @ViewsArgumentDefault(
* id = "query_parameter",
* title = @Translation("Query parameter")
* )
*/
class QueryParameter extends ArgumentDefaultPluginBase {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['query_param'] = array('default' => '');
$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'];
}
}
}

View File

@ -0,0 +1,100 @@
<?php
/**
* @file
* Contains \Drupal\views\Tests\Plugin\argument_default\QueryParameterTest.
*/
namespace Drupal\views\Tests\Plugin\argument_default;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\argument_default\QueryParameter;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests the query parameter argument_default plugin.
*
* @covers \Drupal\views\Plugin\views\argument_default\QueryParameter
*/
class QueryParameterTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '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;
}
}