You are here

function realname_username_alter in Real Name 7

Implements hook_username_alter().

File

./realname.module, line 97
Provides token-based name displays for users.

Code

function realname_username_alter(&$name, $account) {
  static $in_username_alter = FALSE;
  if (empty($account->uid)) {

    // Don't alter anonymous users or objects that do not have any user ID.
    return;
  }

  // Real name was loaded/generated via hook_user_load(), so re-use it.
  if (isset($account->realname)) {
    if (drupal_strlen($account->realname)) {

      // Only if the real name is a non-empty string is $name actually altered.
      $name = $account->realname;
    }
    return;
  }

  // Real name was not yet available for the account so we need to generate it.
  // Because tokens may call format_username() we need to prevent recursion.
  if (!$in_username_alter) {
    $in_username_alter = TRUE;

    // If $account->realname was undefined, then the user account object was
    // not properly loaded. We must enforce calling user_load().
    if ($realname_account = user_load($account->uid)) {
      realname_username_alter($name, $realname_account);
    }
    $in_username_alter = FALSE;
  }
}