Say we have someone’s unique user id (uid), or possess some other information about her, and we wish to collect additional information, and this user is not currently the user that’s logged in (or browsing the web site anonymously), then what do we do ? We use the Drupal function user_load(), companion function of node_load(). The function user_load()
returns a value of the same 'type' as the global-scope $user
variable. It returns an object (worth inspecting). We pass to the node_load() function an associative array containing some or all of the information we have regarding the user we are inquiring about.
// if we have his or her uid $thisUser = user_load(array('uid' => 6)); // if we have his or her name $thisUser = user_load(array('name' => 'Caroline Schnapp'));
Then we can fetch this user’s e-mail address, her last access time, etc.
print t('You can contact @name at <a href="mailto:!email">!email</a>.', array('@name' => $thisUser->name, '!email' => $thisUser->mail));
In the above example, we generated the markup of an anchor element ‘by hand’. We could have used Drupal’s function l() instead :
print t('You can contact @name at !emailURL.', array('@name' => $thisUser->name, '!emailURL' => l($thisUser->mail, 'mailto:' . $thisUser->mail)));
The function l() — l as in link — is usually invoked to generate markup for an internal Drupal link. If you’re not passing to it, as second argument, an internal path, then the function will understand the destination url if and only if it starts with a slash, ‘http’, ‘mailto’ or ‘ftp’. A link that starts with a slash ('/') is called a server-root-relative link, or an absolute link.
The above PHP code will generate the following XHTML markup :
You can contact John at <a href="mailto:john@john.c0m">john@john.c0m</a>.
We can determine when was the last time John visited our website :
print t('@name last visited us !time ago.', array('@name' => $thisUser->name, '!time' => format_interval(time() - $thisUser->login)));
Now, if we want to print the name of a user and make that printed name a link to his user’s page, what do we do ? We can write the anchor element markup by ‘hand’. We can use Drupal’s function l(). Or, we can use a Drupal function of which the sole purpose in life is to accomplish this. That function is a themeable function, meaning that a theme can override it to change what sort of markup is generated. The function is theme_username(). We pass to it a user object, not the user id (uid). The function returns the XHTML markup for the link to a user’s page.
$thisUser = user_load(array('uid' => 6)); print t('@name last visited us !time ago.', array('@name' => theme('username', $thisUser), '!time' => format_interval(time() - $thisUser->login)));
This php code will generate the following XHTML markup :
<a href="/user/6" title="View user profile.">John</a> last visited us 2 days ago.
If we want to know which privileges the user has, we use the function user_access(), and we set the second optional parameter to the $thisUser object.
$thisUser = user_load(array('uid' => $uid)); if (user_access('view uploaded files', $thisUser)) { ... }