4

I added a custom submit function to the user registration form. I want to be able to redirect to the user profile edit. How can I get the uid of the record that is about to be created? global $user doesn't work:

function my_user_register_submit ($form, &$form_state) {
  global $user;
  $form_state['redirect'] = 'user/' . $user->uid . '/edit?registration=1&destination=blahblah'; 
}

Edit:

My use case is this: after registration I want to redirect the user to the "edit profile" page to complete the remaining profile fields. When they "skip" that or complete the form they go to their profile page. However, when a ?destination parameter is passed into the register form I want to carry that value to the "edit profile" page and then direct the user to that destination page when they are done.

1

3 Answers 3

3
+50

Have a look at how login toboggan implements user_register_submit(): logintoboggan_user_register_submit(). It first save the $user in order to retrieve uid later.

function my_user_register_submit($form, &$form_state) {
  //Save $user
  $account = $form['#user'];
  $edit = array_intersect_key((array) $account, $form_state['values']);
  $account = user_save($account, $edit);
  $form_state['user'] = $account;
  $form_state['values']['uid'] = $account->uid;

  //Make the redirection
  $form_state['redirect'] = 'user/' . $account->uid . '/edit?registration=1&destination=blahblah'; 
}

Don't forget to check all the other pieces in the login toboggan's function to not forget any important part.

0
4

Login Toboggan has an option for this whereby you can use %uid as an argument for the redirect URL.

Literally a text box where you'd type /user/!uid/edit

It's also got a whole host of awesome features that are worth checking out :)

5
  • thanks. In this case I would like to do it in code. Login Toboggan is a lot of module to solve one small problem ;-)
    – uwe
    Nov 8, 2011 at 16:23
  • hmm...you won't be able to get the UID of the user that is about to eb created in the form_alter hook, as that user has not yet been created. If you want it to happen every time a use logs in, you should just be able to utilise hook_user_login() with a drupal_goto()?
    – Chapabu
    Nov 8, 2011 at 16:35
  • thanks. I want it to happen after they submit the register form.
    – uwe
    Nov 8, 2011 at 16:38
  • At that point the user has not been created, so there would be no FOOLPROOF way to redirect them. I guess the only way would be to run a query to pull out the most recent UID, and then add one to it. That's really messy though and not really reliable =\ Is there a specific reason why it has to occur at this point?
    – Chapabu
    Nov 8, 2011 at 16:46
  • I updated my question above.
    – uwe
    Nov 8, 2011 at 16:54
1

I think the better option you have is using the Rules module, rather than altering the form.

2
  • rules is great for that, except you have to "force" the redirect which means you'll use the ?destination value
    – uwe
    Nov 8, 2011 at 16:24
  • > I think the better option you have is using the Rules module, rather > than altering the form. And why ? This can be done in 5 lines of php, I can't see no reason why rules module should be a better solution than php to solve such a simple problem. Also do not forget than php can be versionned, reverted etc.. with svn and git and that this is easy to deploy. You'll have to use features or hook to export configuration with rules. Same thing for logintobiggan, that require probably strongarm.
    – yann_yinn
    Apr 11, 2013 at 13:45

Your Answer

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

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