Problem/Motivation
- Realname module alters the display name, but at
user/1234
the username is shown. This is incorrect. - Tons of places in core that show the username, but need to show the users display name.
- Comments do not show DisplayName for registered users.
- Security problem as usernames should never shown on websites. With real names you cannot login and try passwords, with a username you can.
In Drupal 8.0.0 we deprecated \Drupal\Core\Session\AccountInterface::getUsername()
. This is because it was being used for both the display name and getting the username.
The deprecation message is:
* Use \Drupal\Core\Session\AccountInterface::getAccountName() or
* \Drupal\user\UserInterface::getDisplayName() instead.
The docs for getAccountName()
describe the difference:
/**
* Returns the unaltered login name of this account.
*
* @return string
* An unsanitized plain-text string with the name of this account that is
* used to log in. Only display this name to admins and to the user who owns
* this account, and only in the context of the name used to login. For
* any other display purposes, use
* \Drupal\Core\Session\AccountInterface::getDisplayName() instead.
*/
public function getAccountName();
We need to carefully go through the usages and change to getAccountName()
or getDisplayName()
as required. This is not that simple. For example, another issue was created to covert the following code in UserController to getDisplayName().
$this->messenger()
->addWarning($this->t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href=":logout">log out</a> and try using the link again.',
[
'%other_user' => $account->getUsername(),
'%resetting_user' => $reset_link_user->getUsername(),
':logout' => $this->url('user.logout'),
]));
However as this is dealing with the logged in user and a password reset link that works using the account name maybe these should be changed to getAccountName()
.
Proposed resolution
Blocked by #2787871: Properly deprecate getUserName() and use getAccountName() instead
- Fix all places where
getAccountName()
is used, but getDisplayName()
should be used. - Change core to enable a DisplayName by default. That is why tons of tests are failing because of wrong usage.
- Where changing to getDisplayName() add test coverage\
- Ensure getDisplayName() and getAccountName() have good documentation so contrib and custom pick the correct function.
Followup tasks
Allow for configurable truncation length of display name: #2767787: Allow custom truncate username settings
User interface changes
Users display name will be properly shown where appropriate.
API changes
API addition: Entity::getDisplayNameTruncated to get the correct abreviation used in theme_preprocess_username() function.
Data model changes
none