2
$node->content['my_form'] = drupal_get_form('user_login');      

I am creating a form that as outputted on http://site.com/datacollection/the-data-collection

When the user successfully logs in they are redirected to users/USERNAME

In drupal 6, the user would be redirected to the same page they logged in from...How can I reproduce this behavior?

2
  • 2
    have you tried drupal_get_form('user_login_block') instead?
    – Jimajamma
    Commented Dec 12, 2011 at 22:28
  • That worked great, that is actually what I used to solve the problem, maybe you can put an answer in so I can accept it. Commented Dec 13, 2011 at 13:52

4 Answers 4

3
  • Create a action (admin/config/system/actions)

You have to use the token module and find the right token for the needed path. (Maybe someone else can point the right token for this behavior.)

  • Create a trigger (admin/structure/trigger/user)

AFTER A USER HAS LOGGED IN -> redirect to the path

3

There always a hook for that :) _user_login(&$edit, $account)

This hook is called just after a user log in. So you can make a drupal_goto('') if you want.

2

If you're creating a form, you should just be able to set $form_state['redirect'] to the appropriate path in your submit handler. That's what user_login is doing. Just make sure its submit handler runs before yours.

2

As Jimajamma pointed out, the user_login_block form logs the user in and leaves them on the same page. But the form is different from the normal login form.

If you want to present the normal login form on your page, and avoid the redirect, and leave the form on user/login unchanged, then the best way is probably to create your own login form which looks and behaves like the user_login form except for the redirect. This snippet creates such a form:

function custom_user_login_form($form, &$form_state) {
  $form = user_login($form, $form_state);
  $form['#submit'][] = 'user_login_submit';
  $form['#submit'][] = 'custom_form_submit_redirect_current_path';
  return $form;
}

function custom_form_submit_redirect_current_path($form, &$form_state) {
  $form_state['redirect'] = current_path();
}

Then, you would change the form id in your code:

$node->content['my_form'] = drupal_get_form('custom_user_login_form');  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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