3

嗨,作为 drupal 的新手,请指导如何添加新用户,我知道可以使用带有 user_save 的用户模块,但是如何以及在哪里实现它?
我是否必须在下面的自定义表单的提交处理程序中编写此函数-

function registerme_newform($form, &$form_state) 
{
$form = array();
$form['account details'] = array(
'#type' => 'fieldset',
'#title' => t('Account Details'),
'#description' => t('Enter Personal Credentials'),
);
$form['account details']['first name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#default_value' => t('Be sure of your first name'),
'#attributes' => array(
'onblur' => "if (this.value == '') {this.value = 'Be sure of your first name'}",
'onfocus' => "if (this.value == 'Be sure of your first name') {this.value = ''}" 
  , ), 
   ); 
 $form['account details']['last name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#default_value' => t('Be sure of your last name'),
'#attributes' => array(
'onblur' => "if (this.value == '') {this.value = 'Be sure of your last name'}",
'onfocus' => "if (this.value == 'Be sure of your last name') {this.value = ''}" 
   , ), );

 $form['account details']['email'] = array(
'#type' => 'textfield',
'#title' => t('E-Mail'),
'#required' => TRUE,
'#default_value' => t('Email acts as username.Dont forget it!'),
'#attributes' => array(
'onblur' => "if (this.value == '') {this.value = 'Email acts as username.Dont forget 
 it!'}",
'onfocus' => "if (this.value == 'Email acts as username.Dont forget it!')
 {this.value = ''}" 
   , ), 
 );

$form['account details']['password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#maxlength' => 60,
'#size' => 60,
'#required' => TRUE,
'#description' => t('Password should be atleast 6 characters long.'),
);

 $form['home'] = array(
'#type' => 'fieldset',
'#title' => 'Home Address',
'#description' => t('Enter Personal Residential Credentials')
 );

 $form['home']['street'] = array(
'#type' => 'textfield',
'#title' => 'Street Address',
 );

 $form['home']['city'] = array(
'#type' => 'textfield',
'#title' => 'City',
 );

 $form['work'] = array(
'#type' => 'fieldset',
'#title' => 'Work Address',
 '#description' => t('Enter Official Address')
 );

 $form['work']['street'] = array(
'#type' => 'textfield',
'#title' => 'Street Address',
 );

 $form['work']['city'] = array(
'#type' => 'textfield',
'#title' => 'City',
);

 $form['submit'] = array(
 '#type' => 'submit',
 '#value' => t('Register Me'),
 );

 return $form;

 }

 function registerme_newform_validate(&$form, &$form_state)
 {
 if($form_state['values']['account details']['first name']=='Be sure of your first
 name')
 {
  form_set_error('first name',t('Please enter your first name'));
 }
 } 
 function registerme_newform_submit(&$form, &$form_state)
 {
 dsm($form_state); 
  }


还想知道如何在数据库中输入值,我的意思是如何将这些自定义字段添加到数据库中?请指导谢谢。

4

2 回答 2

4

首先请检查名字是否是允许的用户名。我建议您提供另一个字段来输入用户名,因为用户名已被其他人使用并不罕见。然后还要检查没有使用给定电子邮件地址注册的用户。

function registerme_newform_validate(&$form, &$form_state) {
  if($form_state['values']['account details']['first name']=='Be sure of your first
  name') {
  form_set_error('first name',t('Please enter your first name'));
  }
  // TODO: Provide user with a chance to enter a name ( drupal user name )
  // Also do the following two checks and report an error.
  // check using user_load_by_name($form_state['values']['account details']['first anme'])
  // check using user_load_by_mail($form_state['values']['account details']['email'])
} 

接下来在您的提交函数中,将表单值映射到可以传递给 user_save 的数组,如下所示。如果您想确切了解其他字段的映射方式,请使用 devel 模块通过查看已经存在的用户配置文件来检查结构。

function registerme_newform_submit(&$form, &$form_state){
  $new_user = array(
    // The below name field must be unique.
    'name' => $form_state['values']['account details']['name'],
    'pass' => $form_state['values']['account details']['password'],
    'mail' => $form_state['values']['account details']['email'],
    'init' => $form_state['values']['account details']['email'],
    'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['account details']['first name']))),
    'field_last_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['account details']['last name']))),
    // Map all the extra fields to user objects as shown above.
    ...

    'status' => 1,
    'access' => REQUEST_TIME,
    'roles' => $roles,
  );

  // $account returns user object
  $account = user_save(null, $new_user);
  //after user_save you can use the following code to send notification email.
  drupal_mail('user', 'register_no_approval_required', $account->mail, NULL, array('account' => $account), variable_get('site_mail', 'noreply@mysite.com'));
}
于 2012-10-19T17:02:32.697 回答
1

我对这个问题实施了不同的方法。我创建了一个实体表单 ( https://drupal.org/project/entityform ),在其中提示输入关键用户信息。我还安装了 profile2 模块,允许我创建备用配置文件。提交表单时,它会触发规则。该规则允许我创建用户,创建链接配置文件,然后向管理员发送一封电子邮件,其中包含指向新用户条目的链接,允许他们查看和激活。

如果您不想通过 PHP 解决或想在创建时创建其他内容,这是一个不错的选择。

于 2014-01-17T04:27:31.603 回答