Skip to main content
Share Your Experience: Take the 2024 Developer Survey
36 votes
Accepted

Redirect after form submission

In your "submitForm" method write below code $form_state->setRedirect('machine_name'); return; where machine_name is the machine name mentioned in the routing file. I hope this helps .. :)
parth.k's user avatar
  • 1,014
33 votes
Accepted

How to create a redirection programmatically?

If you want to store redirects in database use the module Redirect. This module provides a user interface to add redirects. The redirects are stored in a content entity, which you can also create ...
4uk4's user avatar
  • 100k
31 votes

What function/method can I use to redirect users to a different page?

First (but read on for the correct, better aproach) it's important to note that Drupal 9.2 broke the simple way of doing it: $response = new RedirectResponse($path); $response->send(); You have to ...
Gábor's user avatar
  • 811
24 votes

Redirect after user login

You need to alter the login form and add a submit handler which will take care of the redirection. You cannot use $form_state->setRedirectUrl() directly in the form alter, since it will be ...
pfrenssen's user avatar
  • 1,137
24 votes
Accepted

Redirect Anonymous user to login page

You can test a user's status very early with event subscriber in a custom module that subscribes to KernelEvents::REQUEST. First, you register the event subscriber in mymodule.services.yml in your ...
oknate's user avatar
  • 14.3k
21 votes

How to redirect an anonymous user to the login form after a 403 error?

I've seen that this question was never answered how to do this programmatically. The code actually does work, when placed in an Exception Subscriber: /src/EventSubscriber/RedirectOn403Subscriber.php: &...
4uk4's user avatar
  • 100k
20 votes
Accepted

Redirect to the front page with a different language

You can try this code <?php use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse; $language = Drupal::languageManager()->getLanguage('vi'); $url = Url::fromRoute('&...
MrD's user avatar
  • 6,208
19 votes
Accepted

How can I turn off caching redirect?

A response built from the RedirectReponse class is not cached. So you should not need to set any cache metadata. The problem here is probably the page cache for anonymous requests, which caches all ...
4uk4's user avatar
  • 100k
18 votes

Redirect after user login

Redirecting users after they log in on a Drupal 8 site is not different from how it was done on Drupal 7, except the code needs to be adapted for Drupal 8. In particular: hook_user_login() is not ...
apaderno's user avatar
  • 97.5k
18 votes
Accepted

Force redirect in submit handler even though destination parameter set

The only correct way to do this is to alter the Request object and drop the destination. In your submit callback: \Drupal::request()->query->remove('destination'); Depending on the context, ...
Berdir's user avatar
  • 83.3k
16 votes

Redirect after user login

Bit late to the party but as per https://www.drupal.org/node/2068293#comment-11712455 you can set the destination in hook_user_login() to redirect at the end of the login processing. i.e. /** * ...
Sut3kh's user avatar
  • 161
16 votes
Accepted

How to redirect to frontpage from a controller

Use the route <front> and when you are in a controller use the injected method redirect(): return $this->redirect('<front>');
4uk4's user avatar
  • 100k
13 votes
Accepted

How can I redirect node page for a content type to custom URL?

Here is how I managed to solve this issue, based on the example in drupal.org/node/2013014: I registered Event Subscriber in my_redirect_module.services.yml file: services: ...
Aghiad's user avatar
  • 309
13 votes
Accepted

How to redirect to external URL upon form submission

You'll want to use $form_state->setResponse() instead to utilize the TrustedRedirectResponse you're creating. Something like this should work: public function submitForm(array &$form, ...
hampercm's user avatar
  • 958
11 votes

redirect whole site to HTTPS

RewriteCond %{HTTPS} !=on # This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] # This rule will redirect users from their original ...
rjkunde's user avatar
  • 211
11 votes
Accepted

Disabling Page Cache for Redirect Response

This doesn't exactly answer your question, but I feel it's relevant information. I did something similar, but I also didn't like using the kill switch. In another iteration I used a ...
sonfd's user avatar
  • 7,921
10 votes

How to perform a redirect to custom page after node save or delete?

Basically the code will work for submit action alone, but drupal node save form have many actions, I think you would have logged in as admin and trying it, then follow the code below. if you want ...
Karthikeyan Manivasagam's user avatar
10 votes

Redirect after form submission

If you want to set at buildform you need to use some routing path like. use Drupal\Core\Url; $url = Url::fromRoute('route.path'); $form_state->setRedirectUrl($url);
vgoradiya's user avatar
  • 1,359
10 votes
Accepted

How should I perform a redirect in hook_form_alter()?

use Drupal\Core\Form\FormStateInterface; /** * Implements hook_form_FORM_ID_alter(). */ function [MODULENAME]_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) { //...
harsh_behl_0007's user avatar
9 votes

What function/method can I use to redirect users to a different page?

This can be achieved by leveraging built-in symphonies EventDispatcher Component. All you have to do is create a custom module. Add your services.yml file and provide appropriate service config. ...
Jay Chand's user avatar
  • 425
9 votes

What function/method can I use to redirect users to a different page?

To build on Anu Mathew's response; To add a status code, its just the second param in the RedirectResponse class; use Symfony\Component\HttpFoundation\RedirectResponse; function my_goto($path) { ...
Christian's user avatar
  • 792
9 votes

What function/method can I use to redirect users to a different page?

Never use $response->send() under any circumstances. This results in unpredictable behavior and might fail in different environments, when moving from dev to prod, for example, or in future updates,...
4uk4's user avatar
  • 100k
9 votes

Redirect Anonymous user to login page

First, create a service for your event subscriber in module-name.services.yml Code - services: [MODULE-NAME]_event_subscriber: class: Drupal\MODULE-NAME\EventSubscriber\[Event-Subscriber-...
Ashish Deynap's user avatar
8 votes

Redirect after user login

You can simply do it by using Rules React on: After user has logged in Add action: Redirect >> then use a parameter or type your url.
Shrikant Nakade's user avatar
8 votes

Redirect after user login

In D8 and later, you can use user default page module for this purpose. This module allows you to customize the destination that a user is redirected to after logging in or logged out. You can ...
Pallavi Chaudhury's user avatar
8 votes
Accepted

Redirect using a path

You can also use setRedirectUrl(), which needs a URL object. There are many ways to create one, for example for an arbitrary path: Url::fromUri('base:some/path')
Berdir's user avatar
  • 83.3k
8 votes
Accepted

Redirect a form to an external URL

The method that you missed is $form_state->setResponse(). In combination with a TrustedRedirectResponse object, which exists for security reasons (to prevent users from accidentally being ...
Berdir's user avatar
  • 83.3k
8 votes

Redirect Anonymous user to login page

On Drupal 8.3.3, this code causes an endless redirection. I've fixed that by adding that instead. .. $response = new RedirectResponse('/user/login', 301); $response->send(); ..
G.H.'s user avatar
  • 81
8 votes
Accepted

How to do a RedirectResponse with a destination query parameter?

Better use a route, so that the path processing in Drupal can do its job. If custom.cart_add is the route to /cart/add/{page}, this returns a redirect response with a destination query parameter from ...
4uk4's user avatar
  • 100k
7 votes

Redirect after user login

You can also alter the user login form and add your own custom submit handler to set $form_state redirect, instead of directly redirecting user to your custom URL using hook_user_login. <?php /** ...
Yogesh's user avatar
  • 3,183

Only top scored, non community-wiki answers of a minimum length are eligible