hook_user_presave

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

A user account is about to be created or updated.

This hook is primarily intended for modules that want to store properties in the serialized {users}.data column, which is automatically loaded whenever a user account object is loaded, modules may add to $edit['data'] in order to have their data serialized on save.

Parameters

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

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

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

See also

hook_user_insert()

hook_user_update()

Related topics

6 functions implement hook_user_presave()

2 invocations of hook_user_presave()

File

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

Code

function hook_user_presave(&$edit, $account, $category) {
  // Make sure that our form value 'mymodule_foo' is stored as
  // 'mymodule_bar' in the 'data' (serialized) column.
  if (isset($edit['mymodule_foo'])) {
    $edit['data']['mymodule_bar'] = $edit['mymodule_foo'];
  }
}

Comments

error in code?

the comment in the code above refers to mymodule_bar, but mymodule_bar doesn't show up anywhere in the actual code

it should look like

it should look like this:

<?php
function hook_user_presave(&$edit, $account, $category) {
 
// Make sure that our form value 'mymodule_foo' is stored as 'mymodule_bar'.
 
if (isset($edit['mymodule_foo'])) {
   
$edit['data']['mymodule_bar'] = $edit['mymodule_foo'];
  }
}
?>

Can this be used for validation?

In Drupal 6 we could use hook_user() with $op = 'validate' to run extra validation on the user data.
Can I use this hook to validate user data, if not, wherecan it be done in D7?

I love to know that too.

I love to know that too.

Login or register to post comments