要以编程方式创建具有角色和自定义字段值(例如,名字和姓氏)的用户,可以使用以下代码:
$new_user = array(
'name' => 'xgramp',
'pass' => 'idontwantnoonebutyoutoloveme',
'mail' => 'xgparsons@flyingburritobrothers.la',
'signature_format' => 'full_html',
'status' => 1,
'language' => 'en',
'timezone' => 'America/Los_Angeles',
'init' => 'Email',
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user',
6 => 'member', // role id for custom roles varies per website
),
'field_first_name' => array(
'und' => array(
0 => array(
'value' => 'Gram',
),
),
),
'field_last_name' => array(
'und' => array(
0 => array(
'value' => 'Parsons',
),
),
),
);
$account = user_save(NULL, $new_user);
有关更多详细信息,请参见此博客文章和评论:http :
//codekarate.com/blog/create-user-account-drupal-7-programmatically
array($role_id => 'anything')