You are here

function realname_user_format_name_alter in Real Name 2.x

Same name and namespace in other branches
  1. 8 realname.module \realname_user_format_name_alter()

Implements hook_user_format_name_alter().

File

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

Code

function realname_user_format_name_alter(&$name, AccountInterface $account) {
  static $in_username_alter = FALSE;
  $uid = $account
    ->id();

  // Don't alter anonymous users or objects that do not have any user ID.
  if (empty($uid)) {
    return;
  }

  // Real name was loaded/generated via hook_user_load(), so re-use it.
  if (isset($account->realname)) {
    if (mb_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($uid)) {
      realname_user_format_name_alter($name, $realname_account);
    }
    $in_username_alter = FALSE;
  }
}