We need to be able to import a large group of user accounts, once per school year, then activate the accounts when they are actually needed.

To that end, I wrote a helper module (fix_user_import, for version 6) that accomplishes the task: adds the field to the options fieldset with hook_form_alter, preserves it as a default option, and sets the user account status correctly.

That last action is the reason for this issue. It was not directly possible to set status = 0 (blocked). I tried to do this with hook_user_import_pre_save but status has already been set in supported/user.inc, and setting it again changes the status field to an array and appends the new value at the end. When user_save is called, there's no provision to deal with a field that has been modified in this way. It appears to just be coincidence that status gets set to 1 (active).

I got around it by hooking hook_user on op=insert, and updating the newly-created account to set status appropriately. You'll see that in the code I provided. However, I have to wonder if there's a less kludgy way to deal with this. One possible solution would be to implement the "blocked" option directly in user_import, and I would be willing to submit a patch to that effect if you think it's worthwhile. Or if you can handle the "status" field in some way other than setting it in supported/user.inc?

CommentFileSizeAuthor
fix_user_import.zip1.24 KBgdf

Comments

gdf’s picture

In my initial issue writeup, I probably didn't make it clear enough in the first paragraph that we need to create the accounts with status=blocked. I did say so in the issue title. Sorry for any inadvertent confusion.

agileadam’s picture

Hello there,
I had a similar need on a project recently. My solution was similar to yours except I needed it on a per-user basis. So, using the already-built-in capabilities of setting roles during import, I was able to allow each user record to have a "roles" value. Then, using hook_user I was able to say, "If role = blocked, set status of account to 0 and remove "Blocked" role"

So, my CSV had a new "roles" column that contained either "Blocked" or "Member." It was easy then to match up the Drupal field "Roles" to the CSV column "Roles."

Here's my code:

/**
 * Implementation of hook_user().
 */
function mymodule_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'insert':
    case 'update': 
      // If user in "Blocked" role (rid 5), set status of account to "blocked"
      if ($edit['roles'][5] || $account->roles[5]) {
        unset($account->roles[5]);
        unset($edit['roles'][5]);
        // Setting $account->status = 0 and/or $edit['status'] = 0 and/or $edit['status'] = NULL didn't work
        // so we're updating the record manually, unfortunately
        db_query("UPDATE {users} SET status = %d WHERE uid = %d", 0, $account->uid);
      }
      break;
  }   
}
gisle’s picture

Issue summary: View changes
Status: Active » Closed (duplicate)
Parent issue: » #738158: Users always activated

The Drupal 6 version are no longer supported.

However, there is a similar issue open for the Drupal 7 version. Closing this as a duplicate of #738158: Users always activated.