summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordagmar2019-03-22 16:00:35 (GMT)
committerDamien McKenna2019-03-22 16:00:35 (GMT)
commitd2e586128b51b6955bf5f9af0a7db3288636dca0 (patch)
treed25cb93ec84666e53c850e7fab86bf2a1c7efdb8
parent24a3cab16e9c80fd0aaed1d8f0e8c0f4c2a3c8e1 (diff)
Issue #873872 by dagmar, damiankloip, dawehner, RickJ, DamienMcKenna, tim.plunkett, catch: Allow to limit which exposed operators apply.HEAD7.x-3.x
-rw-r--r--handlers/views_handler_filter.inc46
-rw-r--r--handlers/views_handler_filter_numeric.inc120
-rw-r--r--tests/handlers/views_handler_filter_numeric.test32
3 files changed, 156 insertions, 42 deletions
diff --git a/handlers/views_handler_filter.inc b/handlers/views_handler_filter.inc
index 7220722..172c20b 100644
--- a/handlers/views_handler_filter.inc
+++ b/handlers/views_handler_filter.inc
@@ -135,6 +135,8 @@ class views_handler_filter extends views_handler {
'use_operator' => array('default' => FALSE, 'bool' => TRUE),
'operator_label' => array('default' => '', 'translatable' => TRUE),
'operator' => array('default' => ''),
+ 'limit_operators' => array('default' => FALSE, 'bool' => TRUE),
+ 'available_operators' => array('default' => array()),
'identifier' => array('default' => ''),
'required' => array('default' => FALSE, 'bool' => TRUE),
'remember' => array('default' => FALSE, 'bool' => TRUE),
@@ -274,6 +276,8 @@ class views_handler_filter extends views_handler {
$this->value_submit($form, $form_state);
}
if (!empty($this->options['exposed'])) {
+ $options = &$form_state['values']['options']['expose'];
+ $options['available_operators'] = (!empty($options['use_operator']) && !empty($options['limit_operators'])) ? array_filter($options['available_operators']) : array();
$this->expose_submit($form, $form_state);
}
if ($this->is_a_group()) {
@@ -301,6 +305,21 @@ class views_handler_filter extends views_handler {
public function operator_form(&$form, &$form_state) {
$options = $this->operator_options();
if (!empty($options)) {
+ $available = $this->options['expose']['available_operators'];
+ if ($this->options['expose']['limit_operators'] && count($available)) {
+ foreach ($options as $key => $value) {
+ if (!isset($available[$key])) {
+ unset($options[$key]);
+ }
+ }
+
+ // Make sure we have a valid default value if the current one is
+ // excluded.
+ if (!isset($options[$this->operator])) {
+ // Just choose the first.
+ $this->operator = key($options);
+ }
+ }
$form['operator'] = array(
'#type' => count($options) < 10 ? 'radios' : 'select',
'#title' => t('Operator'),
@@ -511,6 +530,33 @@ class views_handler_filter extends views_handler {
'#title' => t('Required'),
'#default_value' => $this->options['expose']['required'],
);
+
+ $operator_options = $this->operator_options();
+ if (count($operator_options)) {
+ $form['expose']['limit_operators'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Limit operators'),
+ '#description' => t('When checked, the operator will be exposed to the user'),
+ '#default_value' => !empty($this->options['expose']['limit_operators']),
+ '#dependency' => array(
+ 'edit-options-expose-use-operator' => array(1)
+ ),
+ '#description' => t('Restrict which operators will be available to select in the exposed operator form.'),
+ );
+
+ $form['expose']['available_operators'] = array(
+ '#type' => 'checkboxes',
+ '#title' => t('Limit the exposed operators'),
+ '#default_value' => $this->options['expose']['available_operators'],
+ '#prefix' => '<div id="edit-options-expose-available-operators-wrapper"><div id="edit-options-expose-available-operators">',
+ '#suffix' => '</div></div>',
+ '#description' => t('Select which operators will be available to select in the exposed operator form. If none are selected, all the operators listed here will be used.'),
+ '#options' => $operator_options,
+ '#dependency' => array(
+ 'edit-options-expose-limit-operators' => array(1)
+ ),
+ );
+ }
}
else {
$form['expose']['required'] = array(
diff --git a/handlers/views_handler_filter_numeric.inc b/handlers/views_handler_filter_numeric.inc
index c32b864..a338374 100644
--- a/handlers/views_handler_filter_numeric.inc
+++ b/handlers/views_handler_filter_numeric.inc
@@ -165,72 +165,108 @@ class views_handler_filter_numeric extends views_handler_filter {
// not rendered, we can't render dependencies; instead we only
// render the form items we need.
$which = 'all';
+ $limit_operators = !empty($this->options['expose']['limit_operators']) && (count($this->options['expose']['available_operators']) > 0);
+ $use_value = FALSE;
+ $use_minmax = FALSE;
+
if (!empty($form['operator'])) {
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
}
if (!empty($form_state['exposed'])) {
+ $operator_values_with_1_values = $this->operator_values(1);
+ $operator_values_with_2_values = $this->operator_values(2);
+ if ($limit_operators) {
+ // If limit operators is enabled, check that at least one operator
+ // with two values is enabled to display the min max widgets
+ foreach ($operator_values_with_2_values as $operator) {
+ if (isset($this->options['expose']['available_operators'][$operator])) {
+ $use_minmax = TRUE;
+ break;
+ }
+ }
+ // the same for operators with one value
+ foreach ($operator_values_with_1_values as $operator) {
+ if (isset($this->options['expose']['available_operators'][$operator])) {
+ $use_value = TRUE;
+ break;
+ }
+ }
+ }
+ else {
+ $use_minmax = $use_value = TRUE;
+ }
$identifier = $this->options['expose']['identifier'];
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
// exposed and locked.
- $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
+ $which = in_array($this->operator, $operator_values_with_2_values) ? 'minmax' : 'value';
}
else {
$source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
}
}
+ else {
+ $use_minmax = $use_value = TRUE;
+ }
- if ($which == 'all') {
- $form['value']['value'] = array(
- '#type' => 'textfield',
- '#title' => empty($form_state['exposed']) ? t('Value') : '',
- '#size' => 30,
- '#default_value' => $this->value['value'],
- '#dependency' => array($source => $this->operator_values(1)),
- );
- if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
- $form_state['input'][$identifier]['value'] = $this->value['value'];
+ if ($use_value) {
+ if ($which == 'all') {
+ $form['value']['value'] = array(
+ '#type' => 'textfield',
+ '#title' => empty($form_state['exposed']) ? t('Value') : '',
+ '#size' => 30,
+ '#default_value' => $this->value['value'],
+ '#dependency' => array($source => $this->operator_values(1)),
+ );
+ if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
+ $form_state['input'][$identifier]['value'] = $this->value['value'];
+ }
}
- }
- elseif ($which == 'value') {
- // When exposed we drop the value-value and just do value if
- // the operator is locked.
- $form['value'] = array(
- '#type' => 'textfield',
- '#title' => empty($form_state['exposed']) ? t('Value') : '',
- '#size' => 30,
- '#default_value' => $this->value['value'],
- );
- if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
- $form_state['input'][$identifier] = $this->value['value'];
+ elseif ($which == 'value') {
+ // When exposed we drop the value-value and just do value if
+ // the operator is locked.
+ $form['value'] = array(
+ '#type' => 'textfield',
+ '#title' => empty($form_state['exposed']) ? t('Value') : '',
+ '#size' => 30,
+ '#default_value' => $this->value['value'],
+ );
+ if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
+ $form_state['input'][$identifier] = $this->value['value'];
+ }
}
}
if ($which == 'all' || $which == 'minmax') {
- $form['value']['min'] = array(
- '#type' => 'textfield',
- '#title' => empty($form_state['exposed']) ? t('Min') : '',
- '#size' => 30,
- '#default_value' => $this->value['min'],
- );
- $form['value']['max'] = array(
- '#type' => 'textfield',
- '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
- '#size' => 30,
- '#default_value' => $this->value['max'],
- );
- if ($which == 'all') {
- $dependency = array(
- '#dependency' => array($source => $this->operator_values(2)),
+ if ($use_minmax) {
+ $form['value']['min'] = array(
+ '#type' => 'textfield',
+ '#title' => empty($form_state['exposed']) ? t('Min') : '',
+ '#size' => 30,
+ '#default_value' => $this->value['min'],
+ );
+ $form['value']['max'] = array(
+ '#type' => 'textfield',
+ '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
+ '#size' => 30,
+ '#default_value' => $this->value['max'],
);
- $form['value']['min'] += $dependency;
- $form['value']['max'] += $dependency;
+
+ if ($which == 'all') {
+ $dependency = array(
+ '#dependency' => array($source => $this->operator_values(2)),
+ );
+
+ $form['value']['min'] += $dependency;
+ $form['value']['max'] += $dependency;
+ }
}
- if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
+
+ if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min']) && $use_minmax) {
$form_state['input'][$identifier]['min'] = $this->value['min'];
}
- if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
+ if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max']) && $use_minmax) {
$form_state['input'][$identifier]['max'] = $this->value['max'];
}
diff --git a/tests/handlers/views_handler_filter_numeric.test b/tests/handlers/views_handler_filter_numeric.test
index 6cfe2d3..a18e20a 100644
--- a/tests/handlers/views_handler_filter_numeric.test
+++ b/tests/handlers/views_handler_filter_numeric.test
@@ -314,6 +314,38 @@ class ViewsHandlerFilterNumericTest extends ViewsSqlTest {
$this->assertIdenticalResultset($view, $resultset, $this->column_map);
}
+ /**
+ * Tests the limit operators functionality.
+ */
+ public function testFilterNumericExposedLimitOperators() {
+ $filters = $this->getGroupedExposedFilters();
+ $view = $this->getBasicView();
+
+ $available_operators = array('<', '>', 'between');
+
+ $filters['age']['expose'] += array(
+ 'limit_operators' => TRUE,
+ 'available_operators' => drupal_map_assoc($available_operators),
+ );
+
+ $view->display['default']->handler->override_option('filters', $filters);
+
+
+ $this->executeView($view);
+
+ $form = array();
+ $form_state = array();
+ $view->filter['age']->operator_form($form, $form_state);
+
+ $operator = $form['operator'];
+
+ $this->assertTrue(in_array($operator['#default_value'], $available_operators), 'Default value operator found in list of available operators.');
+
+ foreach ($available_operators as $available_operator) {
+ $this->assertTrue($operator['#options'][$available_operator], format_string('@operator found in options', array('@operator' => $available_operator)));
+ }
+ }
+
public function testAllowEmpty() {
$view = $this->getBasicView();