Problem/Motivation

On the user pages (e.g. password reset page - after click on the password reset link), the user name is displayed without format_username(). I think format_username() should alway used when a user name is shown to the user (not in the watchdog log).

In our case, we use a technical name as the user name, which is not known to the user (they login by email). We store the first- and last-name in the profile and use hook_username_alter() to set the correct name to display.

But now, the user does only see the technical name on the password reset page. This is confusing.

Proposed resolution

Use format_username().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

weri created an issue. See original summary.

weri’s picture

Status: Active » Needs review
FileSize
1.28 KB
weri’s picture

weri’s picture

Title: Use format_username() also on the password reset page » Use format_username() on user pages instead of the plain $user->name
Issue summary: View changes
FileSize
3.11 KB

All user names visible for the user uses now format_username().

weri’s picture

Updated patch for core version 7.43.

weri’s picture

Status: Needs review » Needs work

The last submitted patch, 6: drupal-format_name_on_user_pages-2547675-6-D7.patch, failed testing.

Rajender Rajan’s picture

Rajender Rajan’s picture

Status: Needs work » Needs review
SwapS’s picture

@Rajender Rajan : Good to see you in the Issue queue :)

 watchdog('user', 'Sent account cancellation request to %name %email.', array('%name' => format_username($account), '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => format_username($account), '%timestamp' => $timestamp));

No need to use format_username in watchdog messages . That's what
weri has mentioned in the problem statement as well.

SwapS

SwapS’s picture

Status: Needs review » Needs work

The last submitted patch, 6: drupal-format_name_on_user_pages-2547675-6-D7.patch, failed testing.

Rajender Rajan’s picture

Thanks @ SwapS ! Re-submitting patch with all above mentioned changes.

Rajender Rajan’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 13: drupal-format_name_on_user_pages-2547675-12-7.x-dev.patch, failed testing.

stefan.r’s picture

What does this message look like in D8?

SwapS’s picture

Stefan,

Same is the case with D8. Doesn't seems to be formatting username

 $this->logger->notice('User %name used one-time login link at time %timestamp.', array('%name' => $user->getUsername(), '%timestamp' => $form_state->getValue('timestamp')));

SwapS

apaderno’s picture

Drupal 8 uses User::getUsername(), but that is already marked as deprecated for Drupal 9 (i.e. it will be removed before Drupal 9 will be available). Since \Drupal\user\UserInterface::getDisplayName() is the suggested replacement, the same type of change should be done in Drupal 7 too.

I am not sure about the need to change the message in the log: The users who can see it are probably able to see the user account already; I don't see why hiding it to them.

SwapS’s picture

@kiamlaluno - Implementation not only restricted to message in the log but same is approach is followed when showing messages to User.

See below:
$form['message'] = array('#markup' => $this->t('<p>This is a one-time login for %user_name and will expire on %expiration_date.</p><p>Click on this button to log in to the site and change your password.</p>', array('%user_name' => $user->getUsername(), '%expiration_date' => $expiration_date)));

SwapS

stefan.r’s picture

Version: 7.x-dev » 8.2.x-dev

Per the backport policy this ought to be solved in 8.x first :)

apaderno’s picture

Version: 8.2.x-dev » 7.x-dev

Also the tests need to be changed. See https://api.drupal.org/api/drupal/modules%21system%21system.test/7.x, the lines before the number 1251.

    // Submit password reset form.
    $edit = array(
      'name' => $this->user->name,
    );
    $this->drupalPost('user/password', $edit, t('E-mail new password'));
    $mails = $this->drupalGetMails();
    $start = strpos($mails[0]['body'], 'user/reset/'. $this->user->uid);
    $path = substr($mails[0]['body'], $start, 66 + strlen($this->user->uid));

    // Log in with temporary login link.
    $this->drupalPost($path, array(), t('Log in'));

The last line (1251) fails because the login button is not found.

apaderno’s picture

Version: 7.x-dev » 8.3.x-dev

I am sorry. I didn't refresh the page.

SwapS’s picture

Version: 8.3.x-dev » 7.x-dev

Stefan,

Created new issue for taking care of Drupal 8 part of it.

SwapS

stefan.r’s picture

Status: Needs work » Postponed

Postponing this issue on the D8 issue then.

stefan.r’s picture

David_Rothstein’s picture

This looks like a duplicate of #1078894: The User module should use format_username() for system messages or other issues referenced therein.

On the other hand maybe there's some value in fixing the simplest cases first...

David_Rothstein’s picture