I am not sure if this is an actual bug or not, so I assigned category "Support Request"

I've implemented hook_user_format_name_alter in a custom module, and it works perfectly. The newly formatted username that I have defined is being used instead of the default username value everywhere in the application. Everywhere, that is, except the heading on the user profile pages.

EXAMPLE:
Default Username: "123456"
Custom Username Defined by Hook: "customuser"

"customuser" is displaying for my user in the following places:

  • all views that include my user record (example, /admin/people and /admin/content),
  • the administrative toolbar
  • the raw url for my user profile, which shows "/user/customuser" instead of "/user/123456"
  • the heading on my profile's contact tab, which shows "Contact customuser" instead of "Contact 123456"

So, I'm wondering why the title on the main profile page is not affected by hook__user_format_name_alter, especially when the title on the contact page does fulfill the hook's definition. I tried setting up the following RouteSubscriber to override the title on the profile page, but doing so broke something and gave me an "Access Denied" message when trying to access my own profile page (even when testing this out on the admin account):

/**
 * @file
 * Contains \Drupal\MYMODULE\Routing\RouteSubscriber.
 */

// see: https://www.drupal.org/node/2187643

namespace Drupal\MYMODULE\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {
    //Right now, this breaks the user's home page.
    if ($route = $collection->get('entity.user.canonical')) {
      $route->setDefaults(['_title', 'CUSTOM PROFILE TITLE']);
    }
  }

}

Thanks in advance for any assistance.

Comments

xeM8VfDh created an issue. See original summary.

xem8vfdh’s picture

Title: Profile title/heading does not fulfill hook_user_format_name_alter » Profile page's title/heading does not fulfill hook_user_format_name_alter
xem8vfdh’s picture

Issue summary: View changes
xem8vfdh’s picture

thoughts? Beuller?

xem8vfdh’s picture

I'm not sure if this is the source of the issue, but should core/modules/user/templates/username.html.twig call {{ display-name }} instead of {{ name }}, or whatever the attribute variable is that corresponds to user->getDisplayName?

EDIT1: on closer inspection, it looks like the {{ name }} variable should work, as core/modules/user/user.module is properly calling:

$name = $account->getDisplayName();

Furthermore, it looks like the heading name on the profile page is stored in <h1></h1> tags, while the username.html.twig template only writes output between <a></a> or <span></span> tags, so still not entirely sure where the output is coming from.

EDIT2: still not sure if this is a bug, but you can override it via the following module code with:

$current_path = \Drupal::service('path.current')->getPath();
    $current_path_parts = explode('/', $current_path);
    if ($current_path_parts[1] == 'user') {
        $uid = $current_path_parts[2];
        if (is_numeric($uid)) {
            $user = \Drupal\user\Entity\User::load($uid);

            // Replace 'name' substring with display name value
            $variables['title'] = str_replace(
                $user->get('name')->value,
                $user->getDisplayName(),
                $variables['title']
            );
        }
    }
}

If someone knows a cleaner way to do it, please comment here.

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Component: node system » user.module

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

hlykos’s picture

Status: Active » Needs review
StatusFileSize
new597 bytes

It looks that UserController::userTitle still uses the deprecated getUsername() instead of getDisplayName()

Status: Needs review » Needs work

The last submitted patch, 9: 2746065.patch, failed testing.

yogeshmpawar’s picture

Assigned: Unassigned » yogeshmpawar
yogeshmpawar’s picture

Assigned: yogeshmpawar » Unassigned
Status: Needs work » Needs review
StatusFileSize
new625 bytes

Re-roll patch #9 because it's failed to apply on 8.4.x branch.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

The fix makes sense but needs a test to be able to commit it.

berdir’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.21 KB
new1.82 KB

Here is a simple test addition (slightly out of place).

The last submitted patch, 16: profile_page_s-2746065-16-test-only.patch, failed testing. View results

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

aaronbauman’s picture

Category: Support request » Bug report
Status: Needs review » Reviewed & tested by the community

Looks good to me.
We should probably call this a bug?

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Backported to 8.6.x as a non disruptive bug fix.

Committed and pushed bfb8a7da0f to 8.7.x and 88c54681ff to 8.6.x. Thanks!

We should get a follow-up to properly deprecate getUsername() by adding a @trigger_error() and remove all usages in core.

  • alexpott committed bfb8a7d on 8.7.x
    Issue #2746065 by Berdir, yogeshmpawar, hlykos, xeM8VfDh: Profile page's...

  • alexpott committed 88c5468 on 8.6.x
    Issue #2746065 by Berdir, yogeshmpawar, hlykos, xeM8VfDh: Profile page's...
aaronbauman’s picture

There are a few dozen issues open about this already.
Maybe we can glom onto one of those?

This one specifically has a lot of activity already, and will conflict with any patch addressing same: #2787871: Properly deprecate getUserName() and use getAccountName() instead

alexpott’s picture

@aaronbauman +1 to that idea I'll comment on that issue. Thanks for finding it!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.