4

I created a module that implements hook_user_update().
As far as I understand this method is called once a user has edited his data and this has been saved to the database.

The problem I notice is that it is not what really happens with my code: The code is called before.

function mymodule_user_update(&$edit, $account, $category) {
  $username = "username";
  $password = "password";
  $database = "database";

  mysql_connect(localhost,$username,$password);
  @mysql_select_db($database) or die( "Unable to select database");
  $query = "select * from somewhere;";
  $result = mysql_query($query);

  $num = mysql_numrows($result);

  $myFile = "some_file.txt";
  $fh = fopen($myFile, 'w') or die("can't open file");
  $stringData = "now: ".date('Y-m-d H:i:s')." Value is:".mysql_result($result,0);
  fwrite($fh, $stringData);
  fclose($fh);      
}

When I check the result at this point, the values are still the ones before saving the user account.
I thought it would be some kind of database cache but this is supposed to be deleted once you edit the table.

12

Actually, in Drupal 7 user_save() first invokes hook_user_presave(), then saves the user account in the database, and invokes hook_user_update() or hook_user_insert(). It is not possible that hook_user_update() is called before the user account has been saved.
hook_entity_update() and hook_entity_insert() are invoked right after hook_user_update() and hook_user_insert(). Eventual field updates should be executed with those hooks.

// Invoke hook_user_presave() for all modules.
user_module_invoke('presave', $edit, $account, $category);

// …

if (is_object($account) && !$account->is_new) {
  // ...
  $success = drupal_write_record('users', $account, 'uid');
  if ($success === FALSE) {
    // The query failed - better to abort the save than risk further
    // data loss.
    return FALSE;
  }

  // …

  user_module_invoke('update', $edit, $account, $category);
  module_invoke_all('entity_update', $account, 'user');
}
else {
  // …

  $success = drupal_write_record('users', $account);
  if ($success === FALSE) {
    // On a failed INSERT some other existing user's uid may be returned.
    // We must abort to avoid overwriting their account.
    return FALSE;
  }

  // …

  user_module_invoke('insert', $edit, $account, $category);
  module_invoke_all('entity_insert', $account, 'user');

  // …
}

// …

In Drupal 6, user_save() invokes hook_user('update'), saves the user data in the database, and then invokes hook_user('afer_update'); if the user account didn't exist before, it saves the data in the database, and then invokes hook_user('insert').

  • That's exactly what I thought, But when I retrieve the data is not correct... I don't know, is quite weird... – Xavi Colomer Jul 31 '11 at 19:56
  • 1
    Wait, I have extended the user profile with custom fields. It is possible that the workflow goes like: Save User > Hook_user_update > Store extra field values? – Xavi Colomer Jul 31 '11 at 20:24
  • 2
    If you have custom fields added with the Field UI module, those fields should be updated with hook_entity_update(), which is executed after hook_user_update(). – kiamlaluno Jul 31 '11 at 20:34
  • The only thing unclear this article cleared up a little for me is what is the difference is in coding when in hook_presave using $account-­­­­­­­­­­­>data or $edit["variable_name"] ensures the account data is being saved as expected whereas in hook_update implementation , I always assumed account object had allready been been saved and any changes to account in this hook needed to be updated and possibly even saved in this hook, contrary to hook_user_presave. – stefgosselin Sep 1 '12 at 17:36
  • when i use the hook_user_update with an insert query in theme's template.php file , it is not working for me. function garland_user_update(&$edit, $account, $category) { db_insert('user_changes')->fields(array( 'uid' => $account->uid, 'changed' => time(), ))->execute(); exit; } – Ananth May 7 '14 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.