You are here

public function ViewsExposedForm::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Form/ViewsExposedForm.php \Drupal\views\Form\ViewsExposedForm::submitForm()

Form submission handler.

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 FormInterface::submitForm

File

core/modules/views/src/Form/ViewsExposedForm.php, line 184

Class

ViewsExposedForm
Provides the views exposed form.

Namespace

Drupal\views\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Form input keys that will not be included in $view->exposed_raw_data.
  $exclude = [
    'submit',
    'form_build_id',
    'form_id',
    'form_token',
    'exposed_form_plugin',
    'reset',
  ];
  $values = $form_state
    ->getValues();
  foreach ([
    'field',
    'filter',
  ] as $type) {

    /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
    $handlers =& $form_state
      ->get('view')->{$type};
    foreach ($handlers as $key => $info) {
      if ($handlers[$key]
        ->acceptExposedInput($values)) {
        $handlers[$key]
          ->submitExposed($form, $form_state);
      }
      else {

        // The input from the form did not validate, exclude it from the
        // stored raw data.
        $exclude[] = $key;
      }
    }
  }
  $view = $form_state
    ->get('view');
  $view->exposed_data = $values;
  $view->exposed_raw_input = [];
  $exclude = [
    'submit',
    'form_build_id',
    'form_id',
    'form_token',
    'exposed_form_plugin',
    'reset',
  ];

  /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */
  $exposed_form_plugin = $view->display_handler
    ->getPlugin('exposed_form');
  $exposed_form_plugin
    ->exposedFormSubmit($form, $form_state, $exclude);
  foreach ($values as $key => $value) {
    if (!empty($key) && !in_array($key, $exclude)) {
      if (is_array($value)) {

        // Handle checkboxes, we only want to include the checked options.
        // @todo: revisit the need for this when
        //   https://www.drupal.org/node/342316 is resolved.
        $checked = Checkboxes::getCheckedCheckboxes($value);
        foreach ($checked as $option_id) {
          $view->exposed_raw_input[$key][$option_id] = $value[$option_id];
        }
      }
      else {
        $view->exposed_raw_input[$key] = $value;
      }
    }
  }
}