function BooleanOperator::valueForm

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

Options form subform for setting options.

This should be overridden by all child classes and it must define $form['value']

Parameters

array $form: An alterable, associative array containing the structure of the form, passed by reference.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FilterPluginBase::valueForm

File

core/modules/views/src/Plugin/views/filter/BooleanOperator.php, line 195

Class

BooleanOperator
Simple filter to handle matching of boolean values.

Namespace

Drupal\views\Plugin\views\filter

Code

protected function valueForm(&$form, FormStateInterface $form_state) {
  $form['value'] = [];
  if (empty($this->valueOptions)) {
    // Initialize the array of possible values for this filter.
    $this->getValueOptions();
  }
  if ($exposed = $form_state->get('exposed')) {
    // Exposed filter: use a select box to save space.
    $filter_form_type = 'select';
  }
  else {
    // Configuring a filter: use radios for clarity.
    $filter_form_type = 'radios';
  }
  $display_options = 'all';
  $source = ':input[name="options[operator]"]';
  if ($exposed) {
    $identifier = $this->options['expose']['identifier'];
    if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
      $display_options = in_array($this->operator, $this->operatorValues(1)) ? 'value' : 'none';
    }
    else {
      $source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
    }
  }
  if ($display_options === 'all' || $display_options === 'value') {
    $form['value'] = [
      '#type' => $filter_form_type,
      '#title' => $this->value_value,
      '#options' => $this->valueOptions,
      '#default_value' => $this->value,
    ];
    $identifier = $this->options['expose']['identifier'];
    $user_input = $form_state->getUserInput();
    if ($exposed && isset($identifier) && !isset($user_input[$identifier])) {
      $user_input[$identifier] = $this->value;
      $form_state->setUserInput($user_input);
    }
    // If we're configuring an exposed filter, add an - Any - option.
    if (!$exposed || empty($this->options['expose']['required'])) {
      $form['value']['#options'] = [
        'All' => $this->t('- Any -'),
      ] + $form['value']['#options'];
    }
    if ($display_options === 'all') {
      // Setup #states for operators with a value.
      foreach ($this->operatorValues(1) as $operator) {
        $form['value']['#states']['visible'][] = [
          $source => [
            'value' => $operator,
          ],
        ];
      }
    }
  }
}

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