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?
| Comment | File | Size | Author |
|---|---|---|---|
| fix_user_import.zip | 1.24 KB | gdf |
Comments
Comment #1
gdf commentedIn 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.
Comment #2
agileadamHello 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:
Comment #3
gisleThe 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.