You are here

function cf_get_user in Common Functionality 7

Same name and namespace in other branches
  1. 7.2 modules/cf_1x_compatibility/cf_1x_compatibility.module \cf_get_user()

Provide a safe way to get the current user.

This protects the user global from coding accidents.

Why: This is to easy to accidentally do: 'if ($user->uid = 1) {'

The global $user data may also not have contrib modules data included, so accessing $user directly is generally not a good idea. This function seems rather waseteful given its simplicity. It may be better if drupal core would allow $uid to be left empty such that the user_load() function would handle accessing the global $user->id if $uid is empty.

See: http://drupal.org/node/57287 See: http://api.drupal.org/api/drupal/developer--globals.php/global/user/7#co...

Parameters

bool $reset: TRUE to reset the internal cache and load from the database. FALSE (default) to load from the internal cache, if set.

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

A copy of the global variable $user. Changes to this variable will not be retained.

1 call to cf_get_user()
cf_current_user in ./cf.module
Provide a safe way to get the current user.

File

./cf.module, line 86

Code

function cf_get_user($reset = FALSE, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  global $user;
  if (!is_object($user) || !property_exists($user, 'uid')) {
    cf_error_invalid_object($function_history, 'user', WATCHDOG_CRITICAL);
    return FALSE;
  }
  return user_load($user->uid, $reset);
}