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

greenmachine’s picture

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');
jeeba’s picture

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.

dcanetma’s picture

but you're definitely a machine! ;)
Many thanks!

Heine’s picture

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'].

codeglyph’s picture

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.

if($_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.

aerozeppelin’s picture

Thank you, I spent hours trying to figure this out! Thanks Again.