You are here

protected function InOperator::valueForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/filter/InOperator.php \Drupal\views\Plugin\views\filter\InOperator::valueForm()

Options form subform for setting options.

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

Overrides FilterPluginBase::valueForm

See also

buildOptionsForm()

1 call to InOperator::valueForm()
ManyToOne::valueForm in core/modules/views/src/Plugin/views/filter/ManyToOne.php
Options form subform for setting options.
2 methods override InOperator::valueForm()
ManyToOne::valueForm in core/modules/views/src/Plugin/views/filter/ManyToOne.php
Options form subform for setting options.
Name::valueForm in core/modules/user/src/Plugin/views/filter/Name.php
Options form subform for setting options.

File

core/modules/views/src/Plugin/views/filter/InOperator.php, line 173

Class

InOperator
Simple filter to handle matching of multiple options selectable via checkboxes.

Namespace

Drupal\views\Plugin\views\filter

Code

protected function valueForm(&$form, FormStateInterface $form_state) {
  $form['value'] = [];
  $options = [];
  $exposed = $form_state
    ->get('exposed');
  if (!$exposed) {

    // Add a select all option to the value form.
    $options = [
      'all' => $this
        ->t('Select all'),
    ];
  }
  $this
    ->getValueOptions();
  $options += $this->valueOptions;
  $default_value = (array) $this->value;
  $which = 'all';
  if (!empty($form['operator'])) {
    $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'])) {

      // exposed and locked.
      $which = in_array($this->operator, $this
        ->operatorValues(1)) ? 'value' : 'none';
    }
    else {
      $source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
    }
    if (!empty($this->options['expose']['reduce'])) {
      $options = $this
        ->reduceValueOptions();
      if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
        $default_value = [];
      }
    }
    if (empty($this->options['expose']['multiple'])) {
      if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce'])) || isset($this->options['value']['all'])) {
        $default_value = 'All';
      }
      elseif (empty($default_value)) {
        $keys = array_keys($options);
        $default_value = array_shift($keys);
      }
      else {
        $copy = $default_value;
        $default_value = array_shift($copy);
      }
    }
  }
  if ($which == 'all' || $which == 'value') {
    $form['value'] = [
      '#type' => $this->valueFormType,
      '#title' => $this->valueTitle,
      '#options' => $options,
      '#default_value' => $default_value,
      // These are only valid for 'select' type, but do no harm to checkboxes.
      '#multiple' => TRUE,
      // The value options can be a multidimensional array if the value form
      // type is a select list, so make sure that they are counted correctly.
      '#size' => min(count($options, COUNT_RECURSIVE), 8),
    ];
    $user_input = $form_state
      ->getUserInput();
    if ($exposed && !isset($user_input[$identifier])) {
      $user_input[$identifier] = $default_value;
      $form_state
        ->setUserInput($user_input);
    }
    if ($which == 'all') {
      if (!$exposed && in_array($this->valueFormType, [
        'checkbox',
        'checkboxes',
        'radios',
        'select',
      ])) {
        $form['value']['#prefix'] = '<div id="edit-options-value-wrapper">';
        $form['value']['#suffix'] = '</div>';
      }

      // Setup #states for all operators with one value.
      foreach ($this
        ->operatorValues(1) as $operator) {
        $form['value']['#states']['visible'][] = [
          $source => [
            'value' => $operator,
          ],
        ];
      }
    }
  }
}