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)) { ... }
Comments
newbee
Am new to drupal, and still learning about the code and environment. Thanks for the tip on fetching user info, will try to make good use of it.
MP
Most of the time I don’t
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!
www.CupInfo.com.au
thanks
Thanks for these pointers.
Saved my day(s)
Have been searching for that function for days! Really really helpful. Thanks!
Now, if we want to print the
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 e-papierosy 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 e-papierosy 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.
cartier love bracelet
Am new to drupal, and stillIf we want to know which privileges the user has, learning about the code and environment.
Good thoughts and well-written
Twin Fountains EC is a 99-years leasehold Woodlands EC development located at Woodlands Drive 16 in District 25. With expected completion in mid 2016, it comprises of 8 towers with 418 units and stands 14 storeys tall.
Twin Fountains
The condo’s facilities
The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Jurong East.
J Gateway
DUO Residences is also near
DUO Residences is also near to Bugis Junction as well as the buzzling city area. Entertainment for your loved ones and friends is therefore at your fingertips with the full condo facilities as well as the amenities in Bugis.
DUO Residences
Skypark Residences will be
Skypark Residences will be accessible with Sembawang MRT station & Sembawang Bus Interchange. It is also near to Vista Point Shopping Mall, Causeway Point Shopping Mall, Cold Storage, Shop N Save, and many more.
Skyparkresidences