user_register_form

7 user.module user_register_form($form, &$form_state)
8 user.module user_register_form($form, &$form_state)

Form builder; the user registration form.

See also

user_account_form()

user_account_form_validate()

user_register_submit()

Related topics

11 string references to 'user_register_form'

File

modules/user/user.module, line 3681
Enables the user registration and login system.

Code

function user_register_form($form, &$form_state) {
  global $user;

  $admin = user_access('administer users');

  // Pass access information to the submit handler. Running an access check
  // inside the submit function interferes with form processing and breaks
  // hook_form_alter().
  $form['administer_users'] = array(
    '#type' => 'value', 
    '#value' => $admin,
  );

  // If we aren't admin but already logged on, go to the user page instead.
  if (!$admin && $user->uid) {
    drupal_goto('user/' . $user->uid);
  }

  $form['#user'] = drupal_anonymous_user();
  $form['#user_category'] = 'register';

  $form['#attached']['library'][] = array('system', 'jquery.cookie');
  $form['#attributes']['class'][] = 'user-info-from-cookie';

  // Start with the default user account fields.
  user_account_form($form, $form_state);

  // Attach field widgets, and hide the ones where the 'user_register_form'
  // setting is not on.
  field_attach_form('user', $form['#user'], $form, $form_state);
  foreach (field_info_instances('user', 'user') as $field_name => $instance) {
    if (empty($instance['settings']['user_register_form'])) {
      $form[$field_name]['#access'] = FALSE;
    }
  }

  if ($admin) {
    // Redirect back to page which initiated the create request;
    // usually admin/people/create.
    $form_state['redirect'] = $_GET['q'];
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Create new account'),
  );

  $form['#validate'][] = 'user_register_validate';
  // Add the final user registration form submit handler.
  $form['#submit'][] = 'user_register_submit';

  return $form;
}

Comments

Access

...
  $admin = user_access('administer users');

  // If we aren't admin but already logged on, go to the user page instead.
  if (!$admin && $user->uid) {
    drupal_goto('user/' . $user->uid);
  }
...

Checking access before the form builder would be better. Really.

You would think, wouldn't

You would think, wouldn't you? Nope, welcome to the Drupal way! Good luck (you'll need it).

Drupal Way?

There is 'access callback' in hook_menu. And when I call drupal_get_form I expect form array. Sudden redirection is not a pleasant surprise.

So if I want to allow users

So if I want to allow users who don't have "administer users" to create accounts without letting them reset the admin password(!!).. Then how would I go about this? It seems like i'm going to have to copy and paste this whole function in to my module.

Generating the form without giving "administer users" access

So here is my workaround for generating this form without triggering the drupal_goto.

<?php
   
global $user;
   
$perm = &drupal_static('user_access');
   
$perm[$user->uid]['administer users'] = TRUE;   
   
$form = drupal_get_form('user_register_form');
    unset(
$perm[$user->uid]['administer users']);   
?>

It essentially tricks user_access in to thinking i have the permission by setting a static variable. It's ridiculous that I've had to even consider doing this and I would strongly recommend against doing so unless you absolutely have to.

Login or register to post comments