Change the displayed username in Drupal 8 ala Realname

Recovering from surgery finally gave me time to update my last D6 site—a 7 year old private photo and media sharing site with nearly 10,000 nodes and 20+ GB of content—to Drupal 8. Drupal 8 has become a lot more mature lately, to the point where I'm comfortable building a site and not having the foundation rot out from beneath as large ecosystem shifts have mostly settled down.

One thing that I thought would have the simplest implementation actually took a little while to figure out. I needed to have users' full name display instead of their usernames throughout the site. For D6 (and for similar D7 use cases), the easiest way to do this was to enable the Realname module, configure it a tiny bit, and be done with it.

In Drupal 8, however, Realname doesn't yet have a full release (see this issue for progress), and the way usernames are generated has changed slightly (see change record hook_username_alter() changed to hook_user_format_name_alter()).

So it took a few minutes' fiddling around before I came up with the following hook implementation that reformats the user's display name using a 'Name' field (machine name field_name) added to the user entity (you can add the field at /admin/config/people/accounts/fields), but only if there's a value for that user:

<?php
/**
 * Implements hook_user_format_name_alter().
 */
function custom_user_format_name_alter(&$name, $account) {
 
// Load the full user account.
 
$account = \Drupal\user\Entity\User::load($account->id());
 
// Get the full name from field_name.
 
$full_name = $account->get('field_name')->value;
 
// If there's a value, set it as the new $name.
 
if (!empty($full_name)) {
   
$name = $full_name;
  }
}
?>

Note that there's ongoing discussion in the Drupal core issue queue about whether to remove hook_user_format_name_alter(), since there are some caveats with its usage, and edge cases where it doesn't behave as expected or isn't used at all. I'm hoping this situation will be a little better soon—or at least that there will be a Realname release so people who don't like getting their hands dirty with code don't have to :)