3

I'm trying to redirect to the user edit page just after the user logged in from the login block in the sidebar. I implemented hook_user_login, but no luck. It seems to me that drupal_goto() or $edit['rediect'] cannot take precedence over $_GET['destination']

function mymodule_user_login(&$edit, $account){
    if ( user role is employee and user has not completed profile yet ) {
        // both failed to redirect, although my condition is true
        $edit['redirect'] = 'user/'.$account->uid.'/edit'; 
        drupal_goto('user/'.$account->uid.'/edit');
    }
}

I was at my home page ( mydomain.com ) and tried to log in, but when I exit in my hook, I found that the URL was changed to mydomain.com/node?destination=node. My home page path is blank in site information.

[Edit]
My condition for redirect is that if the logged in user has empolyee role and his profile is not completed yet. My usecase is very similar to this, but i don't use content profile.

I think the way of overriding $_GET['destination'] would fix my issue.

  • you can try header("Location: user/".$account->uid."/edit");, of the redirection. – AjitS Sep 17 '12 at 5:29
  • 1
    Is it a drupal way? I think the drupal way for redirect is drupal_goto(). – Sithu Sep 17 '12 at 6:13
  • it is a PHP way to redirect using the header() function. drupal_goto() also uses it. – AjitS Sep 17 '12 at 6:24
  • I know it is a PHP way :) what I mean is that the drupal standard is to use drupal_goto() for redirect. – Sithu Sep 17 '12 at 6:27
5

drupal_goto() will use $_GET['destination'] when it is set. The following is the code used from the function.

  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $destination = drupal_parse_url($_GET['destination']);
    $path = $destination['path'];
    $options['query'] = $destination['query'];
    $options['fragment'] = $destination['fragment'];
  }

Setting $edit['redirect'] inside hook_user_login() doesn't have any effect.
What you can do is changing $_GET['destination'] in hook_user_login(). If you want to be sure the user is redirected to the path you set, you should be sure your hook is executed last, for example by increasing the weight of your module.

  • 1
    I got the fix with a single line of code $_GET['destination'] = 'user/'.$account->uid.'/edit' without drupal_goto() or $edit['rediect']. Thanks for your help. – Sithu Sep 18 '12 at 2:39
0

I think Login Destination module is also giving the same functionality. Apart from that it also provides some more features. It is worth to try.

  • You can customize Login Destination module which will redirect only to specific role also. But I haven't tried with Rules as it will not be possible to identify whether profile is completed or not using Rules – mohit_rocks Sep 17 '12 at 6:29
  • You can also Try LogintoBogaan Module – mohit_rocks Sep 17 '12 at 6:31
2

You have a shortest way,with rules module . update:

Steps

1- Create a new rule

First we have to start a new rule. For this go into the rules admin center, click on "Triggered rules" and on the tab "Add a new rule". Select the event "User has logged in", set the label "On login redirect to account page" and click "Submit". Now we have created a new rule, which is still empty. Every condition and action that we add to this rule, will be evaluated on the event "User has logged in".

2- add your condition in condition section add condition, in Select the condition to add choose Php Execute Custom php code (be sure your PHP filter is installed). check your condition in the condtion section.

3- Create the redirection action

To do so, click on the "Add an action" link, under the section "System", select the action "Page redirect" and click on "Next" button. In case you have the token module installed, the "Page redirect" action allows you to use token replacements. This allows us to place the user's id in the redirect path. So call the action "Redirect to the users account page" and set the path user/[account:uid]. Then activate the checkbox "Force redirecting to the given path, even if a destination parameter is given" - which is necessary, because Drupal always sets a destination parameter to the current page, if a user logs in. So, that our redirection applies, we need to activate this. Check the option, and click on the "Save" button.

  • Can I create my condition with rules and actions? Please see my updated question. I've tried unset, but no success. – Sithu Sep 17 '12 at 6:28
  • yes , I done this work in my project. redirect user to specify page after login(bypass defaut). – Yusef Mohamadi Sep 17 '12 at 6:55
  • Your answer is highly appreciable. But I could not find my condition build in your steps with rules and actions. Can I? – Sithu Sep 17 '12 at 7:29
  • Not need condition. after event user has logged in ** , no need condition , and set **page redirect actions – Yusef Mohamadi Sep 17 '12 at 7:52
  • I need custom redirect only if the logged-in user has empolyee role and his profile is not completed yet. It was stated in my question. – Sithu Sep 17 '12 at 8:15

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.