You are here

public function FormSubmitter::doSubmitForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Form/FormSubmitter.php \Drupal\Core\Form\FormSubmitter::doSubmitForm()

Handles the submitted form, executing callbacks and processing responses.

Parameters

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

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

Return value

null|\Symfony\Component\HttpFoundation\Response If a response was set by a submit handler, or if the form needs to redirect, a Response object will be returned.

Overrides FormSubmitterInterface::doSubmitForm

File

core/lib/Drupal/Core/Form/FormSubmitter.php, line 45

Class

FormSubmitter
Provides submission processing for forms.

Namespace

Drupal\Core\Form

Code

public function doSubmitForm(&$form, FormStateInterface &$form_state) {
  if (!$form_state
    ->isSubmitted()) {
    return;
  }

  // Execute form submit handlers.
  $this
    ->executeSubmitHandlers($form, $form_state);

  // If batches were set in the submit handlers, we process them now,
  // possibly ending execution. We make sure we do not react to the batch
  // that is already being processed (if a batch operation performs a
  // \Drupal\Core\Form\FormBuilderInterface::submitForm).
  if (($batch =& $this
    ->batchGet()) && !isset($batch['current_set'])) {

    // Store $form_state information in the batch definition.
    $batch['form_state'] = $form_state;
    $batch['progressive'] = !$form_state
      ->isProgrammed();
    $response = batch_process();

    // If the batch has been completed and _batch_finished() called then
    // $batch will be NULL.
    if ($batch && $batch['progressive']) {
      return $response;
    }

    // Execution continues only for programmatic forms.
    // For 'regular' forms, we get redirected to the batch processing
    // page. Form redirection will be handled in _batch_finished(),
    // after the batch is processed.
  }

  // Set a flag to indicate the form has been processed and executed.
  $form_state
    ->setExecuted();

  // If no response has been set, process the form redirect.
  if (!$form_state
    ->getResponse() && ($redirect = $this
    ->redirectForm($form_state))) {
    $form_state
      ->setResponse($redirect);
  }

  // If there is a response was set, return it instead of continuing.
  if (($response = $form_state
    ->getResponse()) && $response instanceof Response) {
    return $response;
  }
}