2

I'm looking to redirect users after they login.

The site is multilingual, using English and French.

I've tried using the Login Destination module and using Rules, but it has no options for accessing the language the user has chosen.

On the user edit page, the user can select 'English' or 'French' as their language.

I'm looking, ideally, for a simple rule condition to add, or some PHP.

None of the answers I've found so far have mentioned multilingual sites.

Thanks.

  • Have tried entity has field in Rules condition for User entity? Adding this condition will provide you the field in action. – Vishal Patil Mar 24 '15 at 13:06
  • What is your configuration in admin/config/regional/language/configure ? – M a m a D Mar 24 '15 at 13:11
  • Oh. It's set to URL detection only. I'm guessing that explains it. – Paul Kerrigan Mar 24 '15 at 13:35
  • Just to respond to @VishalPatil, I could not add the 'locale' from the User entity using "Entity has Field" in Rules. – Paul Kerrigan Mar 24 '15 at 13:43
0

May be this can help you.

function custom_module_user_login(&$edit, $account){
  $base_url;
  if($account->language) {
          $redirection = $base_url.'/'.$account->language;  
  } else{
      $redirection = $base_url;
  }

  // Unless there is already a redirection going, or the user is trying to reset his password, we redirect to $redirection.

  if (!is_null($redirection) && (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset')){
    $_GET['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
    $GLOBALS['destination'] = $redirection; // Should we use $edit['redirect'] instead..?
  }
0

If you go to /admin/config/regional/language/configure, you will see an option Follow the user's language preference.. Enable that (and optionally drag it to be at the top) and you're good to go.

  • Not a good answer. This would prevent the user to browse the site in other languages, when following links in the language switcher. Whatever the language code in the URL, the site would always be presented in the user language. – François Nov 13 '16 at 18:52
0

The more elegant and Drupal way is to modify the $form_state['redirect'] variable.

From Drupal core code comments (in includes/form.inc):

$form_state['redirect'] determines where to redirect the user. This can be set either to a string (the path to redirect to), or an array of arguments for drupal_goto().

The key part of the solution is to pass a language argument for drupal_goto(). First add an additional form submit handler for the login form, and modify the redirect variable in this handler. Example:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_user_login_alter(&$form, $form_state, $form_id) {
  $form['#submit'][] = 'mymodule_user_login_form_submit';
}

/**
 * Switches language according to user profile.
 */
function mymodule_user_login_form_submit($form, &$form_state) {
  global $user;
  global $language;

  // Exit quickly if UI is already in correct language.
  if (!(isset($user->language) && isset($language->language) &&
    $user->language != $language->language)) {
    return;
  }

  // Verify user language exists and is enabled.
  $languages = language_list();
  if (!isset($languages[$user->language]) ||
    !$languages[$user->language]->enabled) {
    return;
  }

  // Change destination to translated equivalent, if applicable.
  if (isset($_GET['destination'])) {
    $translations = translation_path_get_translations($_GET['destination']);
    if (array_key_exists($user->language, $translations)) {
      $_GET['destination'] = $translations[$user->language];
    }
  }

  if (isset($form_state['redirect'])) {
    if (is_array($form_state['redirect'])) {
      $form_state['redirect'][1]['language'] = $languages[$user->language];
    } else {
      $form_state['redirect'] = [$form_state['redirect'], [
        'language' => $languages[$user->language],
      ]];
    }
  } else {
    $form_state['redirect'] = [sprintf('user/%d', $user->uid), [
      'language' => $languages[$user->language],
    ]];
  }
}

For further reading, there is a fair amount of information about this topic on this community documentation page from Drupal: How to redirect users after login in drupal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.