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

A user account was created.

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


$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



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().
1 invocation of hook_user_insert()
field_attach_insert in modules/field/field.attach.inc
Save field data for a new entity.


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


function hook_user_insert(&$edit, $account, $category) {
    'myfield' => $edit['myfield'],
    'uid' => $account->uid,


schure’s picture

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.

kubiq’s picture

RULES :D omg, great idea ;) thx

jelo’s picture

Rules only allows to block a user, i.e. I have the entry in my user table. What options do I have if I want to prevent the creation of the user record in the database if certain criteria are met? Rules does not have an option to stop user account creation...

tyler.frankenstein’s picture

Use hook_form_alter() to attach a custom #validate handler to the user registration form. Then in your custom validation handler, check the e-mail value to see if it contains an offending domain(s), if it does just set the form error, and the form will not be allowed to submit.

adammalone’s picture

Also the User Restrictions module

agerard’s picture

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?

theladebug’s picture

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.

alexdma’s picture

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

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:

	// [...]
	$edit['data']['graph_claim'] = $claim;

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

	// [...]
	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...

skparwal’s picture

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

skparwal’s picture

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

function hook_user_insert(&$edit, $account, $category) {
    'fname' => $edit['first_name'],
    'lname' => $edit['last_name'],
    'uid' => $account->uid,
alexdma’s picture

I see, thanks!

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

function mymodule_user_presave(&$edit, $account, $category) {
    if ($account->is_new) {
        $claim = $account->name.'/claims';
        $edit['graph_claim'] = $claim;
TheBarnacle’s picture

Hi alexdma, I reached the same point as yourself in your above comment. But then I discovered that if you just change $account-is_new to zero, then it won't give the SQL error. Hope that helps!

alexdma’s picture

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

   edit->data['graph_claim'] = $claim;
sanduhrs’s picture

This is now part of the entity api: hook_ENTITY_TYPE_insert
See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api...

Abhay Pai’s picture

I have dropdown option in myregistration form and i want to insert the value of dropdown field in on form submission in my custom table, any suggestion how i will achieve this ?

i had tried using the following code but it is giving me error

function insert_record_user_insert(&$edit, $account, $category) {
	'UserTypeId'	=>	'',
	'UserType'		=>	$edit['field_user_type']['und'],