8

I want modify and adding a textfield in my Drupal 7 user/register page. I know that the form is generated by the function user_register_form()

Can i add a textfield in this way?

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

  // If we are already logged on, go to the user page instead.
  if ($user->uid) {
    drupal_goto('user/' . $user->uid);
  }

  // Display login form:
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Username'),
    '#size' => 60,
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#required' => TRUE,
  );

  $form['name']['#description'] = t('Enter your @s username.', array('@s' => variable_get('site_name', 'Drupal')));
  $form['pass'] = array('#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Enter that accompanies your username.'),
    '#required' => TRUE,
  );
$form['address'] = array('#type' => 'textfield',
    '#title' => t('Your address'),
    '#size' => 60,
    '#maxlength' => 125,
    '#required' => TRUE,
  );

  $form['#validate'] = user_login_default_validators();
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));

  return $form;
}
0

4 Answers 4

7

I would rather implement hook_form_FORM_ID_alter() to add the form field.

function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['address'] = array('#type' => 'textfield',
    '#title' => t('Your address'),
    '#size' => 60,
    '#maxlength' => 125,
    '#required' => TRUE,
  );
}

In this way, the form field will be added to the registration form.

0
10

Drupal 7 provides the ability to add custom fields to the User through admin/config/people/accounts/fields. This functionality should already be in core.

6
  • Ok, thank you and for adding textfield to login page?It is a similar way? Commented Jul 21, 2011 at 16:40
  • 1
    Yes, you just have to check off 'Display on user registration form'. or that it is a 'required field'. This is definitely the easiest way of adding a textfield to your user registration form (and being able to save the information to the database).
    – iStryker
    Commented Jul 21, 2011 at 18:01
  • This works best, the only problem I have is that the new fields are not showing up on the edit form of the account when I'm not an admin. How do I achieve this?
    – reptilex
    Commented Feb 5, 2013 at 15:56
  • @reptilex In the config screen of the field you added, there should be a check box labeled Display on user registration form. which should be checked off if you want the user to see it.
    – nmc
    Commented Feb 5, 2013 at 16:05
  • @nmc thanks for the quick answer, am I getting you right? You can either have them on the registration form like I have them now or on the edit form, but not both? I want them to add this information on registration, but they should also be able to edit them. Now the already registered users cannot edit this fields.
    – reptilex
    Commented Feb 5, 2013 at 16:13
4

Example on how to programatically add fields to the user profile and how to avail them, or not, into the User Registration form.

function MYMODULE_enable() {
  // Check if our field is not already created.
  if (!field_info_field('field_myField')) {
    $field = array(
      'field_name' => 'field_myField', 
      'type' => 'text', 
    );

    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
      'field_name' => 'field_myField', 
      'entity_type' => 'user', 
      'label' => 'My Field Name', 
      'bundle' => 'user', 
      // If you don't set the "required" property then the field wont be required by default.
      'required' => TRUE,
      'settings' => array(
        // Here you inform either or not you want this field showing up on the registration form.
        'user_register_form' => 1,
      ),
      'widget' => array(
        'type' => 'textfield',
      ), 
    );

    field_create_instance($instance);
  }
}
1

You may use hook_form_FORM_ID_alter() for adding new fields to the registration form in Drupal 7.

Here is example of adding additional e-mail confirmation field including the validation callback.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function foo_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['account']['mail_confirm'] = array(
    '#type' => 'textfield',
    '#title' => t('Confirm e-mail address'),
    '#maxlength' => EMAIL_MAX_LENGTH,
    '#description' => t('Please confirm your e-mail address.'),
    '#required' => TRUE,
  );
  $form['#validate'][] = 'foo_user_register_form_validate';
}

/**
 * Implements validation callback.
 */
function foo_user_register_form_validate(&$form, &$form_state) {
  if ($form_state['values']['mail'] != $form_state['values']['mail_confirm']) {
    form_set_error('mail_confirm', 'The email addresses must match.');
  }
}

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