Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By jahwe2000 on
Hello,
I am trying to redirect the user to his proper language version of the page he is visiting. Getting the url works fine, but neither setting the http header nor using drupal_goto works. The drupal_set_message is shown though.
Here is my code:
function loginlanguage_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'login' && $account->language) {
$path = drupal_is_front_page() ? '<front>' : $_GET['q'];
$url = i18n_language_property($account->language, 'domain') . url($path, array('language' => $account->language));
drupal_set_message('url: ' . $url);
drupal_goto($url);
}
}
Does anyone have an idea about that? I have tried changing my modules weight already...
kind regards,
Philip
Comments
Need to clear "destination" variables
The drupal_goto setting will override the path in the function's input if a destination value was set previously (in a login form being submitted, for example). This means you need to clear that destination value to be sure that your drupal_goto() will work. Here is code that works for me (used inside of the hook_user() function, just like you have it):
Thanks
Wow after 3 days struggling with my code , you finally give me a light for why couldnt redirect my user. It seems that if you use the login page ( yoursite.com/user) redirect and goto works right, but if you use the login block, they dont want to work anymore.
I couldn't say if you're green...
but you're definitely a machine! ;)
Many thanks!
Bad form
It is really bad style to suddenly interrupt hook execution on events; Drupal may need to do some work to get into a consistent state. Proir to changes to core, such calls would lead to a security vulnerability.
Instead of drupal_goto, change $_REQUEST['destination'].
It's explained in the
It's explained in the function's help page (http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_goto/7)
For D7 the $_GET field responsible for overriding the function arguments is 'destination'.
Since there may be modules that need to perform their own redirection for instance a membership module it's not safe to always clear that field and redirect.
The above will only clear the field & allow your own redirection if it's a regular log in. Adjust according to your own needs of course.
Thank you
Thank you, I spent hours trying to figure this out! Thanks Again.