Our website has a custom hook_user_update() which creates a node. This also triggers a custom hook_node_insert() which happens to need some user information.

The problem is that if we simply use $account = user_load($node->uid) then we get user information which is not up-to-date. This is because user_save() only clears the cache after calling hook_user_update().

In our case it's easy to workaround by using $account = user_load($node->uid, TRUE) but this doens't fix the root cause.

user_save() should clear the user cache before calling the user and entity update/insert hooks. That way, any other code triggered by those hooks will be able to get correct user information with a simple user_load().

If anyone wants to fix this, it's probably important to read through http://drupal.org/node/745668 because there may be a potential performance issue by clearing the user cache early. Perhaps there is a way to update the cache instead of clearing it?

Comments

tstoeckler’s picture

Version: 7.x-dev » 8.x-dev

This should be fixed in Drupal 8 first.

It needs to be checked that this still applies, i.e. that Drupal 8 clears the cache in the same place still.

marcingy’s picture

Version: 8.x-dev » 7.x-dev
Issue summary: View changes
Status: Active » Closed (won't fix)

This appears fixed in d8

{code}
$this->resetCache(array($entity->id()));
$entity->setNewRevision(FALSE);
$entity->postSave($this, TRUE);
$this->invokeFieldMethod('update', $entity);
$this->saveFieldItems($entity, TRUE);
$this->invokeHook('update', $entity);
{code}

I don't see how this can be done in d7 to be honest as it breaks backward computability potential (people base code off none update) so going to close.

nevets’s picture

You could make the call as

$account = user_load($node->uid, TRUE);