Posted by jahwe2000 on September 17, 2009 at 11:19am
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:
<?php
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):
unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);drupal_goto('my/desired/path');
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.
See a < href="http://www.xhelos.com" >Xhelos - Battle Organisms.A Drupal Based RTS Game. Its in early Alpha stage but is testeable, now in english!
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'].
--
The Manual | Troubleshooting FAQ | Tips for posting.
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.
<?phpif($_GET['destination']=='node') unset($_GET['destination']);
drupal_goto("homepage");
?>
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.
www.codeglyph.com