| 7 system.api.php | hook_username_alter(&$name, $account) |
Alter the username that is displayed for a user.
Called by format_username() to allow modules to alter the username that's displayed. Can be used to ensure user privacy in situations where $account->name is too revealing.
Parameters
$name: The string that format_username() will return.
$account: The account object passed to format_username().
See also
Related topics
1 invocation of hook_username_alter()
File
- modules/
system/ system.api.php, line 4172 - Hooks provided by Drupal core and the System module.
Code
function hook_username_alter(&$name, $account) {
// Display the user's uid instead of name.
if (isset($account->uid)) {
$name = t('User !uid', array('!uid' => $account->uid));
}
}
Login or register to post comments
Comments
This is a theme hook!
I was unable to get this hook to fire until I placed it in my template.php file.
Do not treat this like a module hook function.
Actually not (only) a theme hook
As is clear from the source of format_username, this is an alter hook. http://api.drupal.org/api/drupal/includes%21common.inc/function/format_u...
That means it can be implemented either by a module or by the active theme (or any of its base themes). http://api.drupal.org/api/drupal/includes%21module.inc/function/drupal_a...
Example: How to use this hook right
<?phpfunction yourmoduleORtheme_username_alter(&$name, $account) {
/* load the full user object, since $account not always provide all informations */
$user = user_load($account->uid);
/*
field_extract_value(): function provided by field_extract module (thanks adaddinsane!)
*/
if (!empty($user->field_firstname) && !empty($user->field_surname)) {
$name = field_extract_value('user', $user, 'field_firstname') . ' ' . field_extract_value('user', $user, 'field_surname');
}
}
?>
Why is the second argument
Why is the second argument named $account? It seems to only pass the node object, not the user object.