欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Drupal 6 通过代码创建用户? Drupal 7 通过代码创建用户?

shiping1 的头像

Drupal 7请移步到 Drupal 7  通过代码创建用户?

很多情况下,我们需要在对某一动作操作时候,需要自动的创建用户,如ubercart中购买产品时候,如没登陆,自动创建用户。这个是如何实现的呢?看看下面代码。

 

//the user name of the new user
  $user_name = "example";
 
  //the users email address
  $email = "example@example.com";
 
  //set up the user fields using a random 8 character password
  $fields = array(
    'name' => $user_name,
    'mail' => $email,
    'pass' => user_password(8),
    'status' => 1,
  );
 
  //you can give a user roles if necessary based on the role name
  $fields['roles'] = array('test_role');
 
  //the first parameter is left blank so a new user is created
  $account = user_save('', $fields);
 
  // Manually set the password so it appears in the e-mail.
  $account->password = $fields['pass'];
 
  // Send the e-mail through the user module.
  drupal_mail('user', 'register_admin_created', $email, NULL, array('account' => $account), variable_get('site_mail', 'noreply@example..com'));

 
 
 

上面代码基本实现了如何在Drupal 6中通过代码创建用户,但很多时候,用户是有用到 user profile的,就是说有一些附加的字段,那该如何做?看看下面代码:

 

 
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
$user_name = 'my_name';
$email = 'my_name@domain.com';
...
 
$fields = array(
   'name' => $user_name,
   'mail' => $email,
   'pass' => $user_password,
   'status' => 1,
 
   );
$account = user_save('', $fields);
 
if ($account != NULL ) {
  echo "user Id: $account->uid";
$profile_node = content_profile_load('bio', $account->uid, true);
 
if ($profile_node != NULL) {
    echo "node Id: $node_ref->nid";
    return array(
        0 => array('nid' => $profile_node->nid ),
        // 0 => array('USER ONE' => $user_data->field_last_name[0]['value']),
    );
    //it's a published node, 0 if it's un published
    $profile_node->status = 1;
    $profile_node->title = $account->name;
    //1 is promote to home page, 0 is not to promote on home page
    $profile_node->promote = 0;
    $profile_node->uid = $account->uid;
    $profile_node->format = 1; //filtered HTML
    $profile_node->field_emp_id[0]['value'] = '55555';
    $profile_node->field_first_name[0]['value']= 'user1';
    $profile_node->field_last_name[0]['value'] = 'USER ONE';
    $profile_node->field_title[0]['value']= 'Engineer';
 
    if ($profile_node = node_submit($profile_node)) {
        //saves the node
        node_save($profile_node);
     }
 
else {
    echo " no user data found!";
}
}
 

                               

 

Drupal 7 通过代码创建用户?

Drupal 6请移步到 Drupal 6  通过代码创建用户?

很多情况下,我们需要在对某一动作操作时候,需要自动的创建用户,如ubercart中购买产品时候,如没登陆,自动创建用户。这个是如何实现的呢?Drupal 6中的做法,在Drupal 7中会有所改变,看看下面代码。

 

 

 

//This will generate a random password, you could set your own here
  $password = user_password(8);
 
  //set up the user fields
  $fields = array(
    'name' => 'user_name',
    'mail' => 'user_name@example.com',
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    ),
  );
 
  //the first parameter is left blank so a new user is created
  $account = user_save('', $fields);
 
  // If you want to send the welcome email, use the following code
 
  // Manually set the password so it appears in the e-mail.
  $account->password = $fields['pass'];
 
  // Send the e-mail through the user module.
  drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', 'noreply@example..com'));

 

注:你可以附加角色进去,通过role id

上面代码基本实现了如何在Drupal 7中通过代码创建用户,但很多时候,用户是有一些附加的字段,那该如何做?Drupal 7附加字段的方式是需要entity的,这点跟drupal 6有所区别,看看下面代码:

 

 

$new_user = array(
        'name' => 'Username',
        'pass' => 'Password',
        'mail' => 'Email',
        'signature_format' => 'full_html',
        'status' => 1,
        'timezone' => 'America/New_York',
        'init' => 'Email',
        'roles' => 'Roles',
        'field_first_name' =>
            array(LANGUAGE_NONE =>
            array(0 =>
            array('value' => 'First Name'))),
        'field_last_name' =>
            array(LANGUAGE_NONE =>
            array(0 =>
            array('value' => 'Last Name'))),
);
$account= user_save(NULL, $new_user);
 
db_insert('field_data_field_first_name')
    ->fields(array(
    'entity_type' => 'user',
    'bundle' => 'user',
    'deleted' => 0,
    'entity_id' => $account->uid,
    'language' => 'und',
    'delta' => 0,
    'field_first_name_value' => 'First Name',
    ))
    ->execute();
 
db_insert('field_data_field_last_name')
    ->fields(array(
    'entity_type' => 'user',
    'bundle' => 'user',
    'deleted' => 0,
    'entity_id' => $account->uid,
    'language' => 'und',
    'delta' => 0,
    'field_last_name_value' => 'Last Name',
    ))
    ->execute();

 

普通分类: