You are here

public function ExposedFormPluginBase::exposedFormAlter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::exposedFormAlter()

Alters the exposed form.

The exposed form is built by calling the renderExposedForm() method on this class, and then letting each exposed filter and sort handler add widgets to the form. After that is finished, this method is called to let the class alter the finished form.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides ExposedFormPluginInterface::exposedFormAlter

See also

\Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface::renderExposedForm()

\Drupal\views\Form\ViewsExposedForm::buildForm()

File

core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php, line 199

Class

ExposedFormPluginBase
Base class for Views exposed filter form plugins.

Namespace

Drupal\views\Plugin\views\exposed_form

Code

public function exposedFormAlter(&$form, FormStateInterface $form_state) {
  if (!empty($this->options['submit_button'])) {
    $form['actions']['submit']['#value'] = $this->options['submit_button'];
  }

  // Check if there is exposed sorts for this view
  $exposed_sorts = [];
  $exposed_sorts_options = [];
  foreach ($this->view->sort as $id => $handler) {
    if ($handler
      ->canExpose() && $handler
      ->isExposed() && !empty($handler->options['expose']['field_identifier'])) {
      $exposed_sorts[$handler->options['expose']['field_identifier']] = $id;
      $exposed_sorts_options[$handler->options['expose']['field_identifier']] = $handler->options['expose']['label'];
    }
  }
  if (count($exposed_sorts)) {
    $form['sort_by'] = [
      '#type' => 'select',
      '#options' => $exposed_sorts_options,
      '#title' => $this->options['exposed_sorts_label'],
    ];
    $sort_order = [
      'ASC' => $this->options['sort_asc_label'],
      'DESC' => $this->options['sort_desc_label'],
    ];
    $user_input = $form_state
      ->getUserInput();
    if (isset($user_input['sort_by']) && isset($exposed_sorts[$user_input['sort_by']]) && isset($this->view->sort[$exposed_sorts[$user_input['sort_by']]])) {
      $default_sort_order = $this->view->sort[$exposed_sorts[$user_input['sort_by']]]->options['order'];
    }
    else {
      $first_sort = reset($this->view->sort);
      $default_sort_order = $first_sort->options['order'];
    }
    if (!isset($user_input['sort_by'])) {
      $keys = array_keys($exposed_sorts);
      $user_input['sort_by'] = array_shift($keys);
      $form_state
        ->setUserInput($user_input);
    }
    if ($this->options['expose_sort_order']) {
      $form['sort_order'] = [
        '#type' => 'select',
        '#options' => $sort_order,
        '#title' => $this
          ->t('Order', [], [
          'context' => 'Sort order',
        ]),
        '#default_value' => $default_sort_order,
      ];
    }
  }
  if (!empty($this->options['reset_button'])) {
    $form['actions']['reset'] = [
      '#value' => $this->options['reset_button_label'],
      '#type' => 'submit',
      '#weight' => 10,
    ];

    // Get an array of exposed filters, keyed by identifier option.
    $exposed_filters = [];
    foreach ($this->view->filter as $id => $handler) {
      if ($handler
        ->canExpose() && $handler
        ->isExposed() && !empty($handler->options['expose']['identifier'])) {
        $exposed_filters[$handler->options['expose']['identifier']] = $id;
      }
    }
    $all_exposed = array_merge($exposed_sorts, $exposed_filters);

    // Set the access to FALSE if there is no exposed input.
    if (!array_intersect_key($all_exposed, $this->view
      ->getExposedInput())) {
      $form['actions']['reset']['#access'] = FALSE;
    }
  }
  $pager = $this->view->display_handler
    ->getPlugin('pager');
  if ($pager) {
    $pager
      ->exposedFormAlter($form, $form_state);
    $form_state
      ->set('pager_plugin', $pager);
  }
}