Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question

3 Answers

up vote 5 down vote accepted

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.

share|improve this answer
Thank you, you re great!! – gbwebservice Jul 21 '11 at 16:41

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.

share|improve this answer
Ok, thank you and for adding textfield to login page?It is a similar way? – gbwebservice Jul 21 '11 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 Jul 21 '11 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 Feb 5 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 Feb 5 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 Feb 5 at 16:13

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);
  }
}
share|improve this answer

protected by googletorp Jan 23 '12 at 14:13

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

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