You are here

protected function WebformAjaxFormTrait::getFormStateRedirectUrl in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/WebformAjaxFormTrait.php \Drupal\webform\Form\WebformAjaxFormTrait::getFormStateRedirectUrl()

Get redirect URL from the form's state.

Parameters

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

Return value

bool|\Drupal\Core\GeneratedUrl|string The redirect URL or FALSE if the form is not redirecting.

1 call to WebformAjaxFormTrait::getFormStateRedirectUrl()
WebformAjaxFormTrait::submitAjaxForm in src/Form/WebformAjaxFormTrait.php
Submit form #ajax callback.

File

src/Form/WebformAjaxFormTrait.php, line 361

Class

WebformAjaxFormTrait
Trait class for Webform Ajax support.

Namespace

Drupal\webform\Form

Code

protected function getFormStateRedirectUrl(FormStateInterface $form_state) {

  // Always check the ?destination which is used by the off-canvas/system tray.
  if ($this
    ->getRequest()
    ->get('destination')) {
    $destination = $this
      ->getRedirectDestination()
      ->get();
    return strpos($destination, $destination) === 0 ? $destination : base_path() . $destination;
  }

  // ISSUE:
  // Can't get the redirect URL from the form state during an AJAX submission.
  //
  // WORKAROUND:
  // Re-enable redirect, grab the URL, and then disable again.
  $no_redirect = $form_state
    ->isRedirectDisabled();
  $form_state
    ->disableRedirect(FALSE);
  $redirect = $form_state
    ->getResponse() ?: $form_state
    ->getRedirect();
  $form_state
    ->disableRedirect($no_redirect);
  if ($redirect instanceof RedirectResponse) {
    return $redirect
      ->getTargetUrl();
  }
  elseif ($redirect instanceof Url) {
    return $redirect
      ->setAbsolute()
      ->toString();
  }
  else {
    return FALSE;
  }
}