W3cubDocs

/Drupal 8

function hook_user_format_name_alter

hook_user_format_name_alter(&$name, $account)

Alter the username that is displayed for a user.

Called by $account->getDisplayName() to allow modules to alter the username that is displayed. Can be used to ensure user privacy in situations where $account->getDisplayName() is too revealing.

Parameters

string $name: The string that $account->getDisplayName() will return.

$account: The account object the name belongs to.

See also

\Drupal\Core\Session\AccountInterface->getDisplayName()

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/user/user.api.php, line 120
Hooks provided by the User module.

Code

function hook_user_format_name_alter(&$name, $account) {
  // Display the user's uid instead of name.
  if ($account->id()) {
    $name = t('User @uid', array('@uid' => $account->id()));
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!user!user.api.php/function/hook_user_format_name_alter/8.1.x