Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By RealDream-1 on
function hook_user_login($account) {
return new RedirectResponse(url('<front>', array('absolute' => TRUE)));
}
}
this is not work.
Can anyone help me?
Comments
Redirect
You could use header() provide by PHP. Howerver you can only do it in the top of your document cause if there was an output you will not be able to use header().
This is not work, header already sent.
hook_form_alter() not good for conditional redirection after login.
$_GET['destination']='your/path';
not work
$GLOBALS['destination'] = 'your/path'
not work
Drupal 7:
Drupal 8: there is no &$edit parameter
How can conditional redirect after login, when no edit parameter in Drupal 8?
You could use header()
Not in Drupal. There are other mechanisms present (though D8 being new, we are obviously having troubles finding it). You should never use header() in Drupal.
I tried to get this working,
I tried to get this working, but wasn't very successful...
I added a new submit handler to the user login form (no problem here) and there I added a redirection target:
But it didn't work...
So I took a look on the code and the result was:
The functions were called like this: drupal_form_submit > drupal_process_form > drupal_redirect_form
drupal_redirect_form returned a RedirectResponse object to drupal_process_form
drupal_process_form returned it to drupal_form_submit and
drupal_form_submit ignored it!
Maybe it's a Bug! I'm going to investigate further...
Edit: I created a bug report: #2070339: drupal_form_submit ignores return value of drupal_process_form
Btw: The return value of hook_user_login is also ignored.
Has anyone found a way to
Has anyone found a way to redirect a user after login in D8?
I also thought that the RedirectReponse way would work just like RealDream..., but it seems like it's got no effect at all...
I had success using Symfonys
I had success using Symfonys RedirectResponse. For some reason $form_state['redirect] didn't work but kept redirecting to system information front page.
Its worked..
Thank you exlin, its worked for me..:)
Drupal 8.0.2
In Drupal 8.0.2 this returns a 500 server error.
PHP Fatal error: Class 'RedirectResponse' not found ...
Anyone have updated code that works? Sadly the Login Destination module doesn't seem to be ready.
Edit: Found solution at: http://drupal.stackexchange.com/questions/146185/how-to-create-a-redirec...
This worked for me:
Somehow it works. But very slow.
The redirect takes 6-7 seconds on my system. That's too slow for a modern website. I've posted some more explanation in my issue at https://www.drupal.org/node/2655732#comment-10781558. Isn't there really a faster redirect method by "the Drupal way" than with this slow RedirectResponse() object?
resirectresponse issue
while using this, after updating the url it was not working, even after removing the code there is no effect.
I have developed a little
I have developed a little module to redirect user after login or register ... with a configuration page .. https://www.drupal.org/sandbox/benellefimostfa/2676602
ps: he still on sandbox you can get it with "git clone --branch 8.x-1.x benellefimostfa@git.drupal.org:sandbox/benellefimostfa/2676602.git user_redirect"
Worked for me
I used the login redirect, sending the user to "
<front>
" and this module worked for me.However, I then dug a little deeper and found that Login Redirect has already been ported to D8 but it's still just in an issue (i.e. no official release):
https://www.drupal.org/node/2254787
Haven't tried it but you may want to join forces.
You can also alter the user
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.
Adding it in $form_state redirect will make sure that the other submit handlers/login hooks get called.
Like Drupal7 we can't set
$form_state['redirect']
directly, because $form_state is now class object. Checkout FormState::setRedirect() for further details.To redirect to Site Front Page, use '
<front>
' as argument inside\Drupal::url()
, check Page Redirection on Drupal 8 for more details.Yogesh
http://yogeshchaugule.com/blog
This method is probably the
This method is probably the best solution so far. The potential problem of redirecting using the other method with `hook_user_login` is when the user is trying to reset their password, he will not be able to get to the password edit page. user_login_finalize() (which triggers hook_user_login's) is called in UserPasswordResetForm::submitForm().
The above will only apply on the actual user_login submit action.
Using hook_form_FORM_ID_alter
Nice solution @yogeshchaugule8.
Performances can also be improved by implementing hook_form_FORM_ID_alter() instead of the global hook_form_alter().
The code will look like this on modulename.module.
Always being redirected to `/node` after login
Tried using the above code, which seemed to work fine, except that I am always redirected to `/node` after being logged in.
I tried using a customer route, and then I tried to use the examples listed here: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormBuilder....
It looks like the response is including some HTML in its content, which is maybe triggering a Twig_Error_Runtime exception, and so falling back to the default path? I think it's returning a 403 error as well.
Anybody know what would be causing this? Looking at the trace, it seems to be interacting with just default Drupal system modules.
It's a configuration
You can go to /admin/config/system/site-information to edit it. Or you can edit the configuration entity directly to edit it i.e.
\Drupal::config('system.site')->get('page.front'). If you leave it blank it will take every user to the user/login or their own user page when going to ''
I am displaying the login
I am displaying the login form in my own controller. That way it's possible to manipulate the form (and so redirect the user after login) without the non-OO hooks like this:
Change the 'proreos.home' route to whatever destination you need.
Regards
Rainer Feike
Use 'user default page' module for Drupal 8.x
Hi, you can find the module here: https://www.drupal.org/project/user_default_page
User is not logged in on successful registration
I am using a custom submit handler and conditionally set users active or blocked. I am using the redirection to user page for active users in the submit handlers, but not sure why the user is not getting the logged in access and the redirection lands user to the login page. Below is the code:
I expect that on successful registration, if I set the user to active, the user should be able to access the user page.
Any help would be highly appreciated.
@akishankar, You can check if
@akishankar, You can check if immediate login is available (after registration) in your site (in admin => config => people => account settings (settings to check "Who can register accounts?", "Require e-mail verification when a visitor creates an account.")). If not, user session is not started and because of which users are getting redirected to login page as user profile page is accessible only to logged in users unless granted to anonymous users. In this case, you'll need to programmatically initiate user session before redirecting him to user profile page.
Yogesh
http://yogeshchaugule.com/blog
Use the 'Rules' module
Rules is in alpha right now for D8, so you may well want to use one of the solutions above as of the time I'm writing this, but in case anyone is still having this problem in the future, please note that you can use the Rules module to add this redirect. You won't have to write any code either, which is usually a plus. :)
hook_user_login and request_stack service
If you need to check something on $account prior the redirect (and you don't want to assume the login form is used => like if one use simplesaml or such external auth) you could use the hook_user_login and the request_stack service in such a way :
pay attention not to break
pay attention not to break the password reset process with the hook_login ! ...
http://www.webstanz.be
I was using Drupal Rules to
I was using Drupal Rules to redirect after login which was working well until updating to Drupal 8.4.0. After updating the password reset process is getting redirected after clicking 'Log In', meaning it is broken. I am not able to get it to work using a custom module.
Any ideas here?
The code I have tried is:
Example to redirect to front page after login
I confirm above method working.
If you want to redirect to the frontpage after logging in:
Thank you for sharing codes
it worked perfectly
Thanks!
This code worked perfectly for me.
I have to redirect the home
I have to redirect the home page to user/login when i do it on config/system/site-information (replace /node by /user/login)
when the user is login in it redirect to user/uid, i have to redirect to the front page after logging in. but it doest't woek with the config u have changed! what should i do it my case?
Where to put this?
Please forgive my ignorance, but where should I put this code?
Put it in module_name.module
module_name.module where module_name is the name of your custom module to add this functionality.
Error if it is not front
Hello @flyke for your code
It works with front, but if I try a different page (/node/xx or http://example.org/node/xx) I get this error. Can you give to me any tip on way, please? Thanks :)
</pre>
//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong
Solution for not front
Hello
I found the solution. Just in case anybody needs it :)
//trying to answer one question for each one that i make.
//this way, drupal will be more friendly and strong
Login Destination Module Dev Release Works For Me
I downloaded and installed the Login Destination module (dev release for D8) and it seems to work just fine. I'm surprised this is not in core.
The current dev build of
The current dev build of Login Destination breaks your site when you enable it (as of 6/1/18). So that is not a solution
How about just "/user/login
How about just "/user/login?destination=/your-destination"? It works for me.
I'm also looking in to using Rules to redirect
https://www.drupal.org/node/298506
But with a twist ... record which landing page the user registered onto the site with. This way I know which users are interested in what topics, initially. This part I have to figure out yet.
Rules 3.0-alpha2
Just gave a test to Rules 3.0-alpha2 with Drupal 8.5.x, it does not conflict neither with password reset nor with set destination (e.g. redirect when the user meets a 403, with /user/login?destination=whatever).
Rules configuration: React on event: User has logged in, Action: Page redirect.
Rules for D8
I would second some of the other comments-the Rules module does this quite easily, and offers a lot of flexibility. As a benefit, the UI is fairly straightforward.
How about setting up a custom
How about setting up a custom login/logout-link in a twig-template (e.g. page.html.twig) like this:
Replace
{{ path('<current>') }}
with a/
to redirect to the front-page.The OP was close, here is how to do it
This works on a couple different sites we have built in D8.
You can use what ever URL logic in that Redirect Response.
A second way is to create an Event Subscriber (will always log people to their destination even if they are already logged in) like this:
In your_module.services.yml put:
Then create the EventSubscriber
John Ouellet
Sales Engineering Manager
https://thinktandem.io/
Wrap in module?
Maybe it'd be nice to wrap this up in a module. Seems like an atrocious amount of work for a deceivingly simple task ...
hook_user_login
thanks for the tip labboy, the simple one (without EventSubscriber)
it works perfectly combined with secure login
How to handle password setting of first time user login
Hi labboy thanks for the above info.
I have used the first method to redirect the users to corresponding form after login successfully. But I want an exception for it. I want the first time users who are coming from their email links to reset their passwords to not get redirected to form instead allow them to set their pwd and after that redirect them to form. How can this be achieved.
Alan D's comment below look
Alan D's comment below look correct for answering this question.
John Ouellet
Sales Engineering Manager
https://thinktandem.io/
Nice pattern :)
Nice pattern :)
Thanks @labboy0276
Role based redirect with hook_user_login() inc. reset check.
Seems to work nicely when we've used this without any recorded issues. This example checks both the route and user role to conditionally redirect.
Alan Davison
The above code works. This
The above code works. This helped a lot. Thanks Davison.
This module is another option
This module is another option
https://www.drupal.org/project/redirect_after_login
Please try
Please try
drupal 8 redirect user after login
Was there a reason that you
Was there a reason that you load the user rather than just using the wrapper?
Note small API change from 8.6.x
hook_user_login(\Drupal\user\UserInterface $account)
Alan Davison
Doing a redirect in hook_user
Doing a redirect in hook_user_login isn't a great idea as this is a hook called by core and other things can use/rely on it. A redirect in here can interrupt the flow. It can also break logins and cause very hard to debug issues!
True but...
I was working on a way to get around the "other things" because many other options are breaking in the newer versions of 8. Some other re-direct modules do a lot of leg-work that is either no longer relevant and/or just out-right hogging resources. I put together this simple logic into a custom module for handling the login processes:
I have been using this successfully with Drupal 8.8.3 and it does not break my REST login requests from another module I built, re-directs correctly for new account creations, and then re-directs to a different page on a regular site login.
I use Rules.
I use Rules.
Event: User has logged in (rules_user_login)
Action: Page redirect
Done and working perfectly.