The builder function for the user registration form is user_register_form(), whose essential code is the following one:
$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;
}
}
The most important part of user_account_form() is the following one:
// Only show name field on registration form or user can change own username.
$form['account']['name'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#description' => t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, apostrophes, and underscores.'),
'#required' => TRUE,
'#attributes' => array('class' => array('username')),
'#default_value' => (!$register ? $account->name : ''),
'#access' => ($register || ($user->uid == $account->uid && user_access('change own username')) || $admin),
'#weight' => -10,
);
$form['account']['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#maxlength' => EMAIL_MAX_LENGTH,
'#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.'),
'#required' => TRUE,
'#default_value' => (!$register ? $account->mail : ''),
);
// Display password field only for existing users or when user is allowed to
// assign a password during registration.
if (!$register) {
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25,
'#description' => t('To change the current user password, enter the new password in both fields.'),
);
$register
is set by user_account_form()
using the following code:
$account = $form['#user'];
$register = ($form['#user']->uid > 0 ? FALSE : TRUE);
If you just need to show those three fields, then it is sufficient that you call user_account_form()
, setting $form
with the first two lines I reported for user_register_form()
.
$form['#user'] = drupal_anonymous_user();
$form['#user_category'] = 'register';
user_account_form()
has its own validation handler (user_account_form_validate()); you don't need to write code for validating the input given from the user.
To re-use a field created from a function, you need to call that function. In the case of $form['account']['name']
, if I use the following code in my form build, I would not obtain anything: the field will not be created, nor would I get the field defined by that form builder simply for the fact I am using the same string that function uses.
$form['account']['name'] = array();