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.
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | profile_page_s-2746065-16.patch | 1.82 KB | berdir |
| #16 | profile_page_s-2746065-16-test-only.patch | 1.21 KB | berdir |
| #12 | profile_page_s-2746065-12.patch | 625 bytes | yogeshmpawar |
| #9 | 2746065.patch | 597 bytes | hlykos |
Comments
Comment #2
xem8vfdh commentedComment #3
xem8vfdh commentedComment #4
xem8vfdh commentedthoughts? Beuller?
Comment #5
xem8vfdh commentedI'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:
If someone knows a cleaner way to do it, please comment here.
Comment #7
berdirComment #9
hlykos commentedIt looks that UserController::userTitle still uses the deprecated getUsername() instead of getDisplayName()
Comment #11
yogeshmpawarComment #12
yogeshmpawarRe-roll patch #9 because it's failed to apply on 8.4.x branch.
Comment #15
berdirThe fix makes sense but needs a test to be able to commit it.
Comment #16
berdirHere is a simple test addition (slightly out of place).
Comment #19
aaronbaumanLooks good to me.
We should probably call this a bug?
Comment #20
alexpottBackported 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.Comment #23
aaronbaumanThere 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
Comment #24
alexpott@aaronbauman +1 to that idea I'll comment on that issue. Thanks for finding it!