| 5 user.module | user_load( |
| 6 user.module | user_load( |
| 7 user.module | user_load($uid, $reset = FALSE) |
| 8 user.module | user_load($uid, $reset = FALSE) |
Loads a user object.
Drupal has a global $user object, which represents the currently-logged-in user. So to avoid confusion and to avoid clobbering the global $user object, it is a good idea to assign the result of this function to a different local variable, generally $account. If you actually do want to act as the user you are loading, it is essential to call drupal_save_session(FALSE); first. See Safely impersonating another user for more information.
Parameters
$uid: Integer specifying the user ID to load.
$reset: TRUE to reset the internal cache and load from the database; FALSE (default) to load from the internal cache, if set.
Return value
A fully-loaded user object upon successful user load, or FALSE if the user cannot be loaded.
See also
47 calls to user_load()
File
- modules/
user/ user.module, line 361 - Enables the user registration and login system.
Code
function user_load($uid, $reset = FALSE) {
$users = user_load_multiple(array($uid), array(), $reset);
return reset($users);
}
Login or register to post comments
Comments
Getting values of user define (custom) fields.
If you have defined custom fields for the user, e.g. firstname, lastname, phone, address, city, etc. and want to get their values, e.g. to display the full username in a block, use $user_fields. For example,
<?php
global $user;
$user_fields = user_load($user->uid);
$firstname = $user_fields->field_firstname['und']['0']['value'];
$lastname = $user_fields->field_lastname['und']['0']['value'];
print t("Welcome: " . $firstname. ' ' . $lastname) ;
?>
I think the recommend way is
I think the recommend way is to use field_get_items() function instead of get the value by array.
See also:
http://drupal7ish.blogspot.com/2011/02/proper-way-to-get-field-values.html
Oooh, that's my Drupal 7 blog
Oooh, that's my Drupal 7 blog - there's a better blog entry now with a little function to help you, here:
http://drupal7ish.blogspot.com/2011/03/getting-field-data-out-of-entitie...
And now there's a module with
And now there's a module with a couple of very useful functions for extracting field data from entities:
http://j.mp/piHpfH
Fly my pretties...
No role information returned?
So I have this code in a template:
<?php$thisuser = user_load(array('uid' => '11812'));
?>
which, as far as I understand it, should return a user object for the user with the uid of 11812. And that object should contain an array of roles for that user in this variable:
<?php$thisuser->roles
?>
But that array doesn't exist.
I've traced through the user_load() function in modules/user/user.module with a debugger, and this code should populate the $user object with that roles array:
<?php
$result = db_query('SELECT * FROM {users} u WHERE '. implode(' AND ', $query), $params);
if ($user = db_fetch_object($result)) {
$user = drupal_unpack($user);
$user->roles = array();
if ($user->uid) {
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
}
else {
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
}
$result = db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d', $user->uid);
while ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name;
}
user_module_invoke('load', $user_info, $user);
}
else {
$user = FALSE;
}
?>
Everything's fine up through this line:
<?php$user = drupal_unpack($user);
?>
but then, when we hit the next line, which should initialize an empty "roles" array in the $user object, nothing happens:
<?php$user->roles = array();
?>
No "roles" array shows up in the object at all. The subsequent db queries work okay, and return the right role information, so this while loop:
<?phpwhile ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name;
}
?>
has the proper values in $role->rid and $role->name each time through the loop, but $user->roles still doesn't exist, and the assignments here have no effect.
I'm beating my head against this - I can't figure out what's going on. I know that I can just query the db directly in order to get a user's roles if I really must, but why wouldn't the user_load() code work?
Anyone know what's going on?
Thanks.
Username to UID
In Drupal 6 you had the option of specifying a UID or username string as a param in user_load. Now there is only the option of using the UID.
How would you go about getting the UID from username in D7?
user_load_by_name($name)
Hey RobW,
Checkout the user_load_by_name function.
Friendly reminder to NOT assign results of user_load to $user
If you're using user_load to load a user object, make sure that you don't assign the results to a variable name "$user" just incase you included the global $user variable at some point in that function. You will get some pretty unexpected results as you are overwriting who is currently logged in.
I recommend using something like $account instead.