0

I'm trying to redirect users with certain roles to different pages on login. Sample code is below:

function MYMODULE_user_login(&$edit, $account) {
  // Allow users to reset their passwords
  if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
    // Admin
    if (in_array('administrator', $account->roles)) {
      $redirect = 'admin/hud';
    }
    $edit['redirect'] = $redirect;
    dpm($edit);
    dpm($redirect);
  }
}

This is based on drupal.org login hook documentation about "How to redirect users after login in drupal?" and the answer to "Custom redirect on custom page for admin user".

The problem is that when I actually log in with a user having the given role, I am redirected to /user, not my custom path. The results for dpm() for $redirect and $edit->redirect show the path specified in my code, so why am I not getting redirected there?

I had this working in Rules, where there is a Force redirect checkbox. But I am trying to move the code out of rules because it relies on a lot of custom PHP to check various conditions.

1

$edit['redirect'] = $redirect; doesn't stop other operations being executed I think.. The only thing I can think is to stop them with drupal_goto, but you should check you won't break anything else or not:

function MYMODULE_user_login(&$edit, $account) {
  // Allow users to reset their passwords
  if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
    // Under 18
    if (in_array('administrator', $account->roles)) {
      $redirect = 'admin/hud';
    }

    drupal_goto($redirect);
  }
} 
0

There are too many ways to redirect users after login on different pages with their specific roles.I think you should use the rules module to achieve this. There are several ways mentioned on the d.org Because using the custom code might not break something.

0

Drupal always redirects to the user page (by default) ,so you can do this. Customize User Page using Panels and the Page modules by adding a custom php block where you place a drupal_goto(). Then create your custom user page. You can create a switcher using GET arguments in order to redirect especifically where you want. Using access rules (within panels module on custom user variation) you can specify wich role or condition will be needed to perform the custom redirect.

There are many ways to do this either by site-building with core modules or a number of different contributed modules, or by building a custom module implementing the Drupal API. Be warned, the login block sometimes behaves differently than the login page. Also, make sure you test the password reset path which can be affected.

You can use this hook to redirect users to a different page on login, but you will have to make sure the password reset functionality is not impaired. Check this comment. Or look at the node number 683696 at drupal.org. There are modules like: login_redirect Check how these modules work and make your own way out.

Thank You

0

In the end, after checking a number of sites and reading the documentation several times, this is the code that worked for me.

if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {

// Stop the login block from redirecting back to the page the user logged in from.
unset($_GET['destination']);

// Admin
if (in_array('administrator', $account->roles)) {
  $redirect = 'admin/hud';
}
$edit['redirect'] = $redirect;

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.