Alter new user name after submit but before db insertion
shineshadow - June 29, 2009 - 02:08
I'm trying to build a module that can grab the new user info after the submit in order to add a 4 character random number at the end of the username and then insert it into the db.
I've tried $form and $form_state which has the new user data in the #parameter key but the db insertion has taken place before these arrays are made available to my module.
Is there another array or resource available to my module that will allow me to intercept the new user data before it gets inserted into db?

hook_user
Will hook_user do the trick?
Hook_user certainly looks
Hook_user certainly looks promising. Doing some testing now...thanks.
How will the user know their
How will the user know their user name though?
email
The users don't register, the owner(admin) of the organization registers the users using their real names. The random number is to seperate users with the same name in the organization. Once the admin registers them, they are notified by email of their username.
AFAIK drupal does this
AFAIK drupal does this natively.
1st user: blueflowers
2nd user: blueflowers-0
Fourth Wall Media
Toronto, Canada
Not in my experiance
There may be a module that does that but the core user.module simply notifies you that the user name has already been taken. in either case, everyone knows everyone else in this project's organization and it wouldn't be fair for some users to use a clean name and others to have a number at the end. So in the spirit of fairness, everyone will have a supplementary number attached to the user name.
However I'm having a problem with hook_user. I changed the [name] key value to my modified username in the stdClass Object within $account when the $op == 'insert', but the unchanged username still found it's way into the db. I then changed the [name] key value regardless of the $op value and still can't beat the sql insert to the punch. If I can't catch, modify and insert this username change, I'll have to scrap the whole idea.
Ampersands
The first thing to check is that you have ampersands (&'s) in the hook's arguments. It should be:
<?php hook_user($op, &$edit, &$account, $category = NULL) { // do stuff here } ?>not
<?php hook_user($op, $edit, $account, $category = NULL) { // do stuff here } ?>The ampersands mean that the argument is passed by reference.
Use Login Toboggan?
I don't know if you've thought of this already, but the Login Toboggan module enables users to log in with either their email address or their username. That would mean that users don't have to remember their partially random username. (If you combine this with the Real Name module and the core Profile module, users wouldn't even have to be aware of their user name. They could sign in with their email address and the site could refer to them just by their real name.)
Thanks for the reminder
I've used login toboggan module in the past but had forgotten about it. Thanks for the reminder. I will definitely add it to my modules. It will make life easier for users that are used to using their email on other sites and plus, like you said, they don't have to worry about the username.
I'm a little reluctant to use the Real Name module because the author is looking for a new maintainer.
I have the ampersands next to the args in the hook_user. When I save a new user, the modified username even shows up in the drupal message confirming the modified users creation but the original username still goes in the db. The code is as follows:
function ss_get_form_data_user($op, &$edit, &$account, $category = NULL) {
if($op == 'insert') {
$rand_num = rand(1000, 9999);
$orig_name = $account->name;
$account->name = ''.$orig_name.' '.$rand_num.'';
}
return $account;
}
It's probably something silly.
op == 'submit'
I haven't tested this, but try using $op == 'submit' instead. Drupal API advertises that it can "modify the account before it gets saved." Other than that, nothing springs to mind, and I haven't had any luck yet while poking around the Drupal core code for hints.
Anyone else?
Not hook_user
I don't think hook_user is what I want. I've unset $account->name no matter what the $op is. This should completely eliminate the username all together. However, as before, the username is sitting pretty in the db after submitting the new user. This tells me that hook_user has no effect on it. I'll have to keep searching the hooks for one that effects the array that gets inserted into the db. Thanks for your suggestions though.
Is hook_user
OK. Finally found some info here http://drupal.org/node/124489#comment-248280. That sent me in the right direction. I needed to make my changes to the $edit array and not the $account object. I also had to do my changes when $op == 'validate'. The only other $op value during new user creation is 'insert' and that is AFTER the user content is inserted into the db.
switch ($op) {
case 'validate':
$rand_num = rand(1000, 9999);
$orig_name = $edit['name'];
$edit['name'] = ''.$orig_name.' '.$rand_num.'';
break;
}
It does pay not to give up. Finally:) Thanks again for your suggestions everyone...