View source
<?php
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
class NumericFilter extends FilterPluginBase {
protected $alwaysMultiple = TRUE;
protected function defineOptions() {
$options = parent::defineOptions();
$options['value'] = array(
'contains' => array(
'min' => array(
'default' => '',
),
'max' => array(
'default' => '',
),
'value' => array(
'default' => '',
),
),
);
return $options;
}
function operators() {
$operators = array(
'<' => array(
'title' => $this
->t('Is less than'),
'method' => 'opSimple',
'short' => $this
->t('<'),
'values' => 1,
),
'<=' => array(
'title' => $this
->t('Is less than or equal to'),
'method' => 'opSimple',
'short' => $this
->t('<='),
'values' => 1,
),
'=' => array(
'title' => $this
->t('Is equal to'),
'method' => 'opSimple',
'short' => $this
->t('='),
'values' => 1,
),
'!=' => array(
'title' => $this
->t('Is not equal to'),
'method' => 'opSimple',
'short' => $this
->t('!='),
'values' => 1,
),
'>=' => array(
'title' => $this
->t('Is greater than or equal to'),
'method' => 'opSimple',
'short' => $this
->t('>='),
'values' => 1,
),
'>' => array(
'title' => $this
->t('Is greater than'),
'method' => 'opSimple',
'short' => $this
->t('>'),
'values' => 1,
),
'between' => array(
'title' => $this
->t('Is between'),
'method' => 'opBetween',
'short' => $this
->t('between'),
'values' => 2,
),
'not between' => array(
'title' => $this
->t('Is not between'),
'method' => 'opBetween',
'short' => $this
->t('not between'),
'values' => 2,
),
'regular_expression' => array(
'title' => $this
->t('Regular expression'),
'short' => $this
->t('regex'),
'method' => 'op_regex',
'values' => 1,
),
);
if (!empty($this->definition['allow empty'])) {
$operators += array(
'empty' => array(
'title' => $this
->t('Is empty (NULL)'),
'method' => 'opEmpty',
'short' => $this
->t('empty'),
'values' => 0,
),
'not empty' => array(
'title' => $this
->t('Is not empty (NOT NULL)'),
'method' => 'opEmpty',
'short' => $this
->t('not empty'),
'values' => 0,
),
);
}
return $operators;
}
public function operatorOptions($which = 'title') {
$options = array();
foreach ($this
->operators() as $id => $info) {
$options[$id] = $info[$which];
}
return $options;
}
protected function operatorValues($values = 1) {
$options = array();
foreach ($this
->operators() as $id => $info) {
if ($info['values'] == $values) {
$options[] = $id;
}
}
return $options;
}
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['value']['#tree'] = TRUE;
$which = 'all';
if (!empty($form['operator'])) {
$source = ':input[name="options[operator]"]';
}
if ($exposed = $form_state
->get('exposed')) {
$identifier = $this->options['expose']['identifier'];
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
$which = in_array($this->operator, $this
->operatorValues(2)) ? 'minmax' : 'value';
}
else {
$source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
}
}
$user_input = $form_state
->getUserInput();
if ($which == 'all') {
$form['value']['value'] = array(
'#type' => 'textfield',
'#title' => !$exposed ? $this
->t('Value') : '',
'#size' => 30,
'#default_value' => $this->value['value'],
);
foreach ($this
->operatorValues(1) as $operator) {
$form['value']['value']['#states']['visible'][] = array(
$source => array(
'value' => $operator,
),
);
}
if ($exposed && !isset($user_input[$identifier]['value'])) {
$user_input[$identifier]['value'] = $this->value['value'];
$form_state
->setUserInput($user_input);
}
}
elseif ($which == 'value') {
$form['value'] = array(
'#type' => 'textfield',
'#title' => !$exposed ? $this
->t('Value') : '',
'#size' => 30,
'#default_value' => $this->value['value'],
);
if ($exposed && !isset($user_input[$identifier])) {
$user_input[$identifier] = $this->value['value'];
$form_state
->setUserInput($user_input);
}
}
if ($which == 'all' || $which == 'minmax') {
$form['value']['min'] = array(
'#type' => 'textfield',
'#title' => !$exposed ? $this
->t('Min') : '',
'#size' => 30,
'#default_value' => $this->value['min'],
);
$form['value']['max'] = array(
'#type' => 'textfield',
'#title' => !$exposed ? $this
->t('And max') : $this
->t('And'),
'#size' => 30,
'#default_value' => $this->value['max'],
);
if ($which == 'all') {
$states = array();
foreach ($this
->operatorValues(2) as $operator) {
$states['#states']['visible'][] = array(
$source => array(
'value' => $operator,
),
);
}
$form['value']['min'] += $states;
$form['value']['max'] += $states;
}
if ($exposed && !isset($user_input[$identifier]['min'])) {
$user_input[$identifier]['min'] = $this->value['min'];
}
if ($exposed && !isset($user_input[$identifier]['max'])) {
$user_input[$identifier]['max'] = $this->value['max'];
}
if (!isset($form['value'])) {
$form['value'] = array(
'#type' => 'value',
'#value' => NULL,
);
}
}
}
public function query() {
$this
->ensureMyTable();
$field = "{$this->tableAlias}.{$this->realField}";
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($field);
}
}
protected function opBetween($field) {
if ($this->operator == 'between') {
$this->query
->addWhere($this->options['group'], $field, array(
$this->value['min'],
$this->value['max'],
), 'BETWEEN');
}
else {
$this->query
->addWhere($this->options['group'], db_or()
->condition($field, $this->value['min'], '<=')
->condition($field, $this->value['max'], '>='));
}
}
protected function opSimple($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value['value'], $this->operator);
}
protected function opEmpty($field) {
if ($this->operator == 'empty') {
$operator = "IS NULL";
}
else {
$operator = "IS NOT NULL";
}
$this->query
->addWhere($this->options['group'], $field, NULL, $operator);
}
protected function opRegex($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, 'REGEXP');
}
public function adminSummary() {
if ($this
->isAGroup()) {
return $this
->t('grouped');
}
if (!empty($this->options['exposed'])) {
return $this
->t('exposed');
}
$options = $this
->operatorOptions('short');
$output = $options[$this->operator];
if (in_array($this->operator, $this
->operatorValues(2))) {
$output .= ' ' . $this
->t('@min and @max', array(
'@min' => $this->value['min'],
'@max' => $this->value['max'],
));
}
elseif (in_array($this->operator, $this
->operatorValues(1))) {
$output .= ' ' . $this->value['value'];
}
return $output;
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['identifier'])) {
$value =& $input[$this->options['expose']['identifier']];
if (!is_array($value)) {
$value = array(
'value' => $value,
);
}
}
$rc = parent::acceptExposedInput($input);
if (empty($this->options['expose']['required'])) {
$info = $this
->operators();
if (!empty($info[$this->operator]['values'])) {
switch ($info[$this->operator]['values']) {
case 1:
if ($value['value'] === '') {
return FALSE;
}
break;
case 2:
if ($value['min'] === '' && $value['max'] === '') {
return FALSE;
}
break;
}
}
}
return $rc;
}
}