Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The global $user object that exists on each page is not a complete user object, contrary to what one might think. The only time the user object is complete is after user_load() is called, because user_load() fires (calls) all of the hook_user() hooks, allowing modules to add whatever data they wish to the user object. Without user_load() being called, none of the module-added data for the user object will exist.
This task is to add documentation about this inconsistent and potentially misleading situation to the contrib/docs/core.php documentation.
Comments
Comment #1
g011um CreditAttribution: g011um commentedInterestingly, with the addition of some
watchdog()
calls I see thathook_user()
is being fired in my module during a normal request. However, any fields added to the $user object inhook_user()
seem to disappear at some later point (confirmed by creating a page node that simply dumps the contents of $user).Comment #2
brunodboComment #3
brunodboChanged the component to reflect the new component categorization. See http://drupal.org/node/301443.
Comment #4
nterbogt CreditAttribution: nterbogt commentedI've noticed the same thing as g011um. If it's getting fired anyway, why not just write it to the global, because you're not saving any time if it's happening at some point. Either that or just don't ever run it until you need it.
Comment #5
jhodgdonLooks like this should be documented somewhere, or perhaps fixed in the user module? Temporarily assigning to the user module to see if it should be fixed or documented.
Comment #6
jwilson3I am also seeing the same situation as comment #1, It puzzles me why if hook_user() gets run anyway that it doesnt actually update the global user object. I was able to solve this in my module's hook_user() in the following way... I've documented it here in a way that you could drop this in as a completely new module called force_userload.module. Just make sure to set the weight of this module heavier than all others, so it gets run later.
UPDATE: this worked flawlessly on my development machine, but didnt seem to work so great on my production machine. I tried the sample code here #361471: Global $user object should be a complete entity which worked better.
Comment #8
ohnobinki CreditAttribution: ohnobinki commented.