Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Form redirection on submit

By editor , 15 January, 2016
Question

How to redirect a form after submission

Example : (Insert this line in to your form submitForm() methode)

Redirect to the user page ( Redirection on form submit)
$form_state->setRedirect('user.page');

Redirect to a custom page
$form_state->setRedirect('your.module.route.name');

 

Like:

  public function submitForm(array &$form, FormStateInterface $form_state) {
   $form_state->setRedirect('your.module.route.name');
  }

Using a Url Object (use Drupal\Core\Url)

$url = Url::fromRoute('route.name', [
  'arg1' => "value1",
  'arg1' => "value2",
]);
$form_state->setRedirectUrl($url);

Tags

  • Drupal 8
  • Code
  • Form
  • Add new comment
Tips for Form API of Drupal 8
Page Redirection on Drupal 8 / 9
Redirect a user after login. Redirection on Drupal 8.

Comments4

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

zaibatsu (not verified)

10 years 8 months ago

Redirect to a custom page

Greetings,

If i want to redirect a form to a view page? how can i do that?

$form_state->setRedirect('views.route.name'); ? how would i get the route name?

can you please help? Thanks

  • Reply

editor

10 years 6 months ago

In reply to Redirect to a custom page by zaibatsu (not verified)

You can use page redirection…

You can use page redirection like:

return new \Symfony\Component\HttpFoundation\RedirectResponse(\Drupal::url('user.page'));

Page Redirection on Drupal 8

  • Reply

Pavel (not verified)

9 years 7 months ago

When i use $form_state-…

When i use $form_state->setRedirect('<front>'); i get nothing in $_POST array on front page. How can i send post data of form with setRedirect?

  • Reply

Mohit (not verified)

9 years 2 months ago

page redirection

how to redirect node

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer