function hook_user_insert

You are here

7 user.api.php hook_user_insert(&$edit, $account, $category)
8 user.api.php hook_user_insert($account)

A user account was created.

The module should save its custom additions to the user object into the database.

Parameters

$edit: The array of form values submitted by the user.

$account: The user object on which the operation is being performed.

$category: The active category of user information being edited.

See also

hook_user_presave()

hook_user_update()

Related topics

4 functions implement hook_user_insert()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

entity_crud_hook_test_user_insert in modules/simpletest/tests/entity_crud_hook_test.module
Implements hook_user_insert().
openid_user_insert in modules/openid/openid.module
Implements hook_user_insert().
profile_user_insert in modules/profile/profile.module
Implements hook_user_insert().
trigger_user_insert in modules/trigger/trigger.module
Implements hook_user_insert().
2 invocations of hook_user_insert()
field_attach_insert in modules/field/field.attach.inc
Save field data for a new entity.
user_save in modules/user/user.module
Save changes to a user account or add a new user.

File

modules/user/user.api.php, line 252
Hooks provided by the User module.

Code

function hook_user_insert(&$edit, $account, $category) {
  db_insert('mytable')->fields(array(
    'myfield' => $edit['myfield'],
    'uid' => $account->uid,
  ))->execute();
}

Comments

Is there an easier way to, for example, block all users that do not have email addresses from a certain institution?

Edit: Found the Rules module does it. Sorry for the spam, folks.

RULES :D omg, great idea ;) thx

Also the User Restrictions module

I'm relatively clueless about php but trying to add a subscription for all new users, and finding a problem: if admin adds a user, then the $account value is admin. Is it possible to grab or construct the new user object from the $edit object? Does it exist at this point?

The documentation here is a bit misleading. The $edit object is not just the array of form values, but rather the array of form values after stripping any which don't match up to the account object.

So, you can't add extra values to the $form_state array and expect them to be in this $edit object. Only items which match up to the fields in the user object are passed, all others are stripped out.

Does this mean that a hook like this will not work:

<?php
function mymodule_user_insert(&$edit, $account, $category) {
   
$claim = $account->name.'/claims';
   
$edit['graph_claim'] = $claim;
}
?>

I've tried every variant of this, but I could never see a value for ['data']['graph_claim'] for the user just being created.

No dice even after I tried:

<?php
   
// [...]
   
$edit['data']['graph_claim'] = $claim;
?>

When I tried to manually call user_save, things got even worse:

<?php
   
// [...]
   
user_save($account, $edit);
?>

led to:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '65' for key 'PRIMARY': INSERT INTO {users} (uid, name, pass, mail, theme, signature, signature_format, created, access, login, status, timezone, language, picture, init, data) VALUES (
[...]
[:db_insert_placeholder_15] => a:2:{s:11:"graph_claim";s:40:"someguy/claims";} ) in drupal_write_record() (line 7136 of /usr/share/drupal7/includes/common.inc).

On no occasion could I ever perform the seemingly trivial task of adding further data values to a user account that had just been created...

$edit is related to form elements as per the database table field names. You cannot add an another array element to it.

If you want to add an another element to $edit array, then you need to do some change when you save the records like:

<?php
function hook_user_insert(&$edit, $account, $category) {
 
db_insert('mytable')->fields(array(
   
'fname' => $edit['first_name'],
   
'lname' => $edit['last_name'],
   
'uid' => $account->uid,
  ))->
execute();
}
?>

I see, thanks!

Alternatively, I guess it should work if I were to "mimic" the same behaviour using the hook_user_presave ?

<?php
function mymodule_user_presave(&$edit, $account, $category) {
    if (
$account->is_new) {
       
$claim = $account->name.'/claims';
       
$edit['graph_claim'] = $claim;
    }
}
?>

Sorry, I cannot seem to be able to edit the above post. I meant:

<?php
   edit
->data['graph_claim'] = $claim;
?>