drupal_get_current_user(), is there a module that provides some utility functions like this?
crystaldawn - June 15, 2009 - 17:35
I find myself using $user = $_GLOBALS['user']; a lot but in some instances, its fine, but I dont like using it. So I made a utility function that is easier to read that does essentially the same thing. Is there a module kicking around that has small utility functions like the following?
<?php
if (!function_exists('drupal_get_current_user'))
{
//IE: drupal_get_current_user('name'); //Return the currently logged in username if any.
//IE: drupal_get_current_user(); //Get the whole user object
function drupal_get_current_user($field = '')
{
//Return the currently logged in user
$user = $GLOBALS['user'];
if ($field)
{
$field = check_plain($field);
return $user->$field;
}
else
{
return $user;
}
}
}
?>
You can also simple use
You can also simple use
global $user;global $user doesnt seem to
global $user doesnt seem to work all the time. So I have always used the $GLOBALS['user'] instead. It works yes, but it's ugly. Personal preference, I like writing code as though they were sentences and I know many others do as well so they tend to use macro functions like I do.