如何在drupal 7中向用户/注册页面表单添加文本字段?


8

我想在Drupal 7用户/注册页面中修改并添加文本字段。我知道表格是由函数生成的user_register_form()

我可以用这种方式添加文本框吗?

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

Answers:


7

我宁愿实现hook_form_FORM_ID_alter()来添加表单字段。

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

这样,表格字段将被添加到注册表格中。


10

Drupal 7提供了通过来向用户添加自定义字段的功能admin/config/people/accounts/fields。此功能应该已经在核心中。


好的,谢谢您,并在登录页面中添加了文本字段吗?这是类似的方法吗?
gbwebservice 2011年

1
是的,您只需要勾选“在用户注册表上显示”。或者它是“必填字段”。这绝对是将文本字段添加到用户注册表单(并能够将信息保存到数据库)的最简单方法。
iStryker

这是最好的方法,我唯一的问题是,当我不是管理员时,新字段不会显示在帐户的编辑表单中。我该如何实现?
reptilex

@reptilex在添加的字段的配置屏幕中,应该有一个标记为在用户注册表单上显示的复选框如果您希望用户看到它,则应选中该复选框。
nmc

@nmc感谢您的快速回答,我说对了吗?您可以像现在一样在注册表格上拥有它们,也可以在编辑表格上拥有它们,但不能同时使用?我希望他们在注册时添加此信息,但他们也应该能够对其进行编辑。现在,已经注册的用户无法编辑此字段。
reptilex

4

有关如何以编程方式将字段添加到用户配置文件以及如何在“用户注册”表单中使用或不使用它们的示例。

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

您可以使用hook_form_FORM_ID_alter()在Drupal 7中向注册表单添加新字段。

这是添加其他电子邮件确认字段(包括验证回调)的示例。

/**
 * 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.');
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.