NOTE: Role import is natively supported by the UIF module, so there is no need to use the hook technique described in this issue.

ORIGINAL POST:
as per my other post, that dwightaspinwall answered:

Easily done in a hook_uif_post_create and/or hook_uif_post_update funkeyrandy. Just update the user and call user_save(). Hope that helps.
Next time, please open a new issue, since your comment was unrelated. Thanks.

im opening a new issue :)

here is what i have for adding multiple roles to a user, and assigning status:

function uif_example_uif_post_update($account, $user_data, $form_state) {

$roles[]=explode('|',$user_data['roles']);
foreach ($roles as $role)  {
   user_multiple_role_edit(array($account->uid), 'add_role', $role->rid);
	}
} 

ive tested and its not working..nor is a simple example:

	if($user_data['roles']=='4'){
		$name="Member";
	}elseif($user_data['roles']=='5'){
		$name="Manager";
		
	}
	
	
  $new_role[$user_data['roles']] = $name;
    user_save($account->uid, array('roles' => $new_role));

the users import, but the correct role isnt being set

any ideas?
thanks!

Comments

funkeyrandy’s picture

Issue summary: View changes

updated

dwightaspinwall’s picture

This should work for you:

/**
 * Implementation of hook_uif_post_update().
 */
function test_uif_post_update($account, $user_data, $form_state) {
  test_add_roles($account, $user_data['roles']);
}

/**
 * Implementation of hook_uif_post_create().
 */
function test_uif_post_create($account, $user_data, $form_state) {
  test_add_roles($account, $user_data['roles']);
}

/**
 * Add role(s) if any to account if not present.
 */
function test_add_roles($account, $roles) {
  if (isset($roles['roles'])) {
    $role_names = explode('|', $roles);
    foreach ($role_names as $name) {
      $rid = db_query('SELECT rid FROM {role} WHERE name = :name', array(':name' => $name))->fetchField();
      if ($rid) {
        $account->roles[$rid] = $name;
      }
    }
    user_save($account);
  }
}

Substituting your module name for "test" of course. This assumes you want to add roles when creating or updating, and they call the common function.

dwightaspinwall’s picture

Status: Active » Fixed

Moved the above code into uif_example module. Thanks for the inspiration.

funkeyrandy’s picture

Status: Fixed » Active

you are the man! thanks so much!

dwightaspinwall’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

brei9000’s picture

Status: Closed (fixed) » Active

It seems that using user_save($account) in this manner deletes the profile picture.

dwightaspinwall’s picture

Status: Active » Closed (fixed)

Role import is natively supported by UIF now, so you don't need to use the method described in #1 above. Update the module if you haven't already and give it a whirl.

dwightaspinwall’s picture

Issue summary: View changes

update

dwightaspinwall’s picture

Issue summary: View changes

Modify to indicate role import natively supported