Document user_load must be called to get fully realized user object

Public

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.

Get raw version
php
  1. <?php
  2. /**
  3.  * Implementation of hook_user().
  4.  *
  5.  * Ensures global $user gets completely loaded.
  6.  */
  7. function force_userload_user($op, $edit, &$account, $category = NULL) {
  8. global $user;
  9. switch ($op) {
  10. case 'load':
  11. // Optionally, add/modify the $account object eg,
  12. // $account->custom_field = 'mystuff';
  13. if ($user->uid == $account->uid) {
  14. $user = $account;
  15. }
  16. break;
  17. }
  18. }
  19. ?>