3

I have a situation where my users of role 'Operator' are in charge of creating new user accounts. Right now, when they create a new user account it just sends them back to the user/create page and displays a confirmation message.

I would like to have drupal redirect the user to the profile page (user/%) of the user that they just created.

I was looking here (http://drupal.stackexchange.com/questions/14993/how-can-i-do-a-custom-form-redirect-on-the-user-registration-form) but can't figure out how to get the user id of the user that the 'Operator' created and not the user id of the 'Operator' them self.

2 Answers 2

2

Here is Form API solution for that:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function YOUR_MODULE_form_user_register_form_alter(&$form, &$form_state) {
  // Check if user has permission to add new user
  if (user_access('administer users')) {
    $form['#submit'][] = 'YOUR_MODULE_user_register_submit';
  }
}

/**
 * Submit callback
 */
function YOUR_MODULE_user_register_submit(&$form, &$form_state) {
  if (isset($form_state['values']['uid'])) {
    // Redirect to user page
    $form_state['redirect'] = url('user/' . $form_state['values']['uid']);
  }
}

And here is Rules solution:

  1. Add event "After saving a new user account"
  2. Add condition "User has role(s)"site:current-user has role "Operator". I really don't know how to check permission in Rules, so it is not the best solution.
  3. Add action "Page redirect"user/[account:uid]
2
  • should the role of the users I want to be redirected replace the 'administer users'?
    – Mike
    Commented May 22, 2012 at 7:51
  • @Mike, I just tested it for permission "Administer users", which allows to add new users in Drupal. You can add other condition. It depends on your user/create logic.
    – kalabro
    Commented May 22, 2012 at 7:56
1

How about a rule? Check the Rules Module for this task. With this you can define what happens after a user registered. For example a redirect or many other actions.

5
  • Thank Lance but, I tried that. How can I make a rule to redirect to the user they just created? I've done redirection with Rules before but never like this.
    – Mike
    Commented May 22, 2012 at 7:36
  • It works if you just give the path "user". This is the logged profile of the logged in user.
    – Lance
    Commented May 22, 2012 at 7:44
  • I don't want to redirect to the logged in users profile though.
    – Mike
    Commented May 22, 2012 at 7:48
  • Right, but what happens when you choose the redirect action and enter the [account:url] token?
    – Lance
    Commented May 22, 2012 at 8:59
  • I see now. Thanks, I went with hook_form_FORM_ID_alter() approach
    – Mike
    Commented May 22, 2012 at 11:43

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.