function NumericFilter::valueForm

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/filter/NumericFilter.php \Drupal\views\Plugin\views\filter\NumericFilter::valueForm()
  2. 8.9.x core/modules/views/src/Plugin/views/filter/NumericFilter.php \Drupal\views\Plugin\views\filter\NumericFilter::valueForm()
  3. 11.x core/modules/views/src/Plugin/views/filter/NumericFilter.php \Drupal\views\Plugin\views\filter\NumericFilter::valueForm()

Provide a simple textfield for equality.

Overrides FilterPluginBase::valueForm

1 method overrides NumericFilter::valueForm()
Date::valueForm in core/modules/views/src/Plugin/views/filter/Date.php
Add a type selector to the value form.

File

core/modules/views/src/Plugin/views/filter/NumericFilter.php, line 206

Class

NumericFilter
Simple filter to handle greater than/less than filters.

Namespace

Drupal\views\Plugin\views\filter

Code

protected function valueForm(&$form, FormStateInterface $form_state) {
    $form['value']['#tree'] = TRUE;
    // We have to make some choices when creating this as an exposed
    // filter form. For example, if the operator is locked and thus
    // not rendered, we can't render dependencies; instead we only
    // render the form items we need.
    $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'])) {
            // Exposed and locked.
            $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'] = [
            '#type' => 'textfield',
            '#title' => !$exposed ? $this->t('Value') : '',
            '#size' => 30,
            '#default_value' => $this->value['value'],
        ];
        if (!empty($this->options['expose']['placeholder'])) {
            $form['value']['value']['#attributes']['placeholder'] = $this->options['expose']['placeholder'];
        }
        // Setup #states for all operators with one value.
        foreach ($this->operatorValues(1) as $operator) {
            $form['value']['value']['#states']['visible'][] = [
                $source => [
                    'value' => $operator,
                ],
            ];
        }
        if ($exposed && !isset($user_input[$identifier]['value'])) {
            $user_input[$identifier]['value'] = $this->value['value'];
            $form_state->setUserInput($user_input);
        }
    }
    elseif ($which == 'value') {
        // When exposed we drop the value-value and just do value if
        // the operator is locked.
        $form['value'] = [
            '#type' => 'textfield',
            '#title' => !$exposed ? $this->t('Value') : '',
            '#size' => 30,
            '#default_value' => $this->value['value'],
        ];
        if (!empty($this->options['expose']['placeholder'])) {
            $form['value']['#attributes']['placeholder'] = $this->options['expose']['placeholder'];
        }
        if ($exposed && !isset($user_input[$identifier])) {
            $user_input[$identifier] = $this->value['value'];
            $form_state->setUserInput($user_input);
        }
    }
    // Minimum and maximum form fields are associated to some specific operators
    // like 'between'. Ensure that min and max fields are only visible if
    // the associated operator is not excluded from the operator list.
    $two_value_operators_available = $which == 'all' || $which == 'minmax';
    if (!empty($this->options['expose']['operator_limit_selection']) && !empty($this->options['expose']['operator_list'])) {
        $two_value_operators_available = FALSE;
        foreach ($this->options['expose']['operator_list'] as $operator) {
            if (in_array($operator, $this->operatorValues(2), TRUE)) {
                $two_value_operators_available = TRUE;
                break;
            }
        }
    }
    if ($two_value_operators_available) {
        $form['value']['min'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Min'),
            '#size' => 30,
            '#default_value' => $this->value['min'],
        ];
        if (!empty($this->options['expose']['min_placeholder'])) {
            $form['value']['min']['#attributes']['placeholder'] = $this->options['expose']['min_placeholder'];
        }
        $form['value']['max'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Max'),
            '#size' => 30,
            '#default_value' => $this->value['max'],
        ];
        if (!empty($this->options['expose']['max_placeholder'])) {
            $form['value']['max']['#attributes']['placeholder'] = $this->options['expose']['max_placeholder'];
        }
        if ($which == 'all') {
            $states = [];
            // Setup #states for all operators with two values.
            foreach ($this->operatorValues(2) as $operator) {
                $states['#states']['visible'][] = [
                    $source => [
                        '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'])) {
            // Ensure there is something in the 'value'.
            $form['value'] = [
                '#type' => 'value',
                '#value' => NULL,
            ];
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.