FormSubmitter::redirectForm

public FormSubmitter::redirectForm(FormStateInterface $form_state)

Redirects the user to a URL after a form has been processed.

After a form is submitted and processed, normally the user should be redirected to a new destination page. This function figures out what that destination should be, based on the $form_state and the 'destination' query string in the request URL, and redirects the user there.

The result of \Drupal\Core\Form|FormStateInterface::getRedirect() determines where to redirect the user. See the possible return values listed there. If the result is FALSE, then the user will not be redirected.

Here is an example of how to set up a form to redirect to the path 'user':

$form_state->setRedirect('user.page');

And here is an example of how to redirect to 'node/123?foo=bar#baz':

$form_state->setRedirect('entity.node.canonical',
  array('node' => 123),
  array(
    'query' => array(
      'foo' => 'bar',
    ),
    'fragment' => 'baz',
  ),
));

Parameters

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

Return value

\Symfony\Component\HttpFoundation\RedirectResponse|null

Overrides FormSubmitterInterface::redirectForm

See also

\Drupal\Core\Form\FormBuilderInterface::processForm()

\Drupal\Core\Form\FormBuilderInterface::buildForm()

File

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

Class

FormSubmitter
Provides submission processing for forms.

Namespace

Drupal\Core\Form

Code

public function redirectForm(FormStateInterface $form_state) {
  $redirect = $form_state->getRedirect();

  // Allow using redirect responses directly if needed.
  if ($redirect instanceof RedirectResponse) {
    return $redirect;
  }

  $url = NULL;
  // Check for a route-based redirection.
  if ($redirect instanceof Url) {
    $url = $redirect->setAbsolute()->toString();
  }
  // If no redirect was specified, redirect to the current path.
  elseif ($redirect === NULL) {
    $request = $this->requestStack->getCurrentRequest();
    $url = $this->urlGenerator->generateFromRoute('<current>', [], ['query' => $request->query->all(), 'absolute' => TRUE]);
  }

  if ($url) {
    // According to RFC 7231, 303 See Other status code must be used to redirect
    // user agent (and not default 302 Found).
    // @see http://tools.ietf.org/html/rfc7231#section-6.4.4
    return new RedirectResponse($url, Response::HTTP_SEE_OTHER);
  }
}
doc_Drupal
2016-10-29 09:16:50
Comments
Leave a Comment

Please login to continue.