web-dev-qa-db-fra.com

Comment ajouter un champ de texte au formulaire de page utilisateur / inscription dans drupal 7?

Je souhaite modifier et ajouter un champ de texte dans ma page Drupal 7 utilisateur/registre. Je sais que le formulaire est généré par la fonction user_register_form()

Puis-je ajouter un champ de texte de cette façon?

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;
}
8
gbwebservice

Je préfère implémenter hook_form_FORM_ID_alter () pour ajouter le champ du formulaire.

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,
  );
}

De cette façon, le champ du formulaire sera ajouté au formulaire d'inscription.

7
kiamlaluno

Drupal 7 offre la possibilité d'ajouter des champs personnalisés à l'utilisateur via admin/config/people/accounts/fields. Cette fonctionnalité devrait déjà être au cœur.

10
nmc

Exemple sur la façon d'ajouter par programme des champs au profil utilisateur et comment les utiliser, ou non, dans le formulaire d'inscription utilisateur.

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);
  }
}
4
Francisco Luz

Vous pouvez utiliser hook_form_FORM_ID_alter () pour ajouter de nouveaux champs au formulaire d'inscription dans Drupal 7.

Voici un exemple d'ajout d'un champ de confirmation de courrier électronique supplémentaire, y compris le rappel de validation.

/**
 * 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.');
  }
}
1
kenorb