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?

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;
      }
   }
}

Comments

nevets’s picture

You can also simple use global $user;

crystaldawn’s picture

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.