Problem/Motivation

This is mostly a follow-up to #2112679: getUsername() should return the username getDisplayName() for the formatted user name where some minor consensus was reached about removing hook_user_format_name_alter(). See: https://www.drupal.org/node/2112679#comment-10356635

This comes about because we let contrib change the user name through hook_user_format_name_alter() for reasons that have a lot of down-side and not a lot of up-side, mostly inherited from the time before there were fieldable user entities.

Proposed resolution

Remove hook_user_format_name_alter() and keep the account name as something only core can touch.

Remaining tasks

  1. Cogitate.
  2. Confab.
  3. Consense.
  4. Commit.

User interface changes

API changes

Data model changes

Comments

Mile23 created an issue. See original summary.

piyuesh23’s picture

Status: Active » Needs review
StatusFileSize
new9.12 KB

Removed the module_handler service calls from core/modules/user/src/Entity/User.php & core/lib/Drupal/Core/Session/UserSession.php.

There were a few tests implementing this hook as well. Have removed them as well for now. Should be re-thinking on how to get them back in.

Status: Needs review » Needs work

The last submitted patch, 2: 2575213-Removing_user_name_alter_hook-1.patch, failed testing.

The last submitted patch, 2: 2575213-Removing_user_name_alter_hook-1.patch, failed testing.

nlisgo’s picture

Assigned: Unassigned » nlisgo
Issue tags: +Needs reroll
nlisgo’s picture

I can't discover which commit to revert to so that this patch applies so I am going to try to manually reapply the patch.

nlisgo’s picture

Assigned: nlisgo » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new8.39 KB

Wasn't too much trouble to manually re-apply. Let's see what testbot says!

Status: Needs review » Needs work

The last submitted patch, 7: remove-2575213-7.patch, failed testing.

The last submitted patch, 7: remove-2575213-7.patch, failed testing.

nlisgo’s picture

Assigned: Unassigned » nlisgo

I'm going to investigate the failing tests.

nlisgo’s picture

Assigned: nlisgo » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.71 KB
new82.22 KB

Addressing failing tests.

andypost’s picture

patch contains a lot of unrelated changes (pull fresh 8.0.x)

Status: Needs review » Needs work

The last submitted patch, 11: remove-2575213-11.patch, failed testing.

nlisgo’s picture

Assigned: Unassigned » nlisgo

Apologies. I'm on a plane now. Will try and work on it on my travels.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new2.43 KB
new8.94 KB

#11 + removal of @see

+++ b/core/lib/Drupal/Core/Session/AccountInterface.php
@@ -137,9 +137,7 @@ public function getAccountName();
    * @see hook_user_format_name_alter()

removed too

nlisgo’s picture

Assigned: nlisgo » Unassigned

Thanks @andypost. I did manage to clean it up but you beat me to it. I'll unassign.

cilefen’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

This hook is not deprecated. We need at least a change record.

subhojit777’s picture

There is an issue #2572821: Fix documentation on hook_user_format_name_alter(), should we continue to put out effort there?

hass’s picture

This case confuses me a bit. You may overlooked realname module. You are breaking it hereby isn't it?

How should a developer change the display-name of a user with tokens if we cannot alter it any longer? I'm using it together with address field module and use the firstname and lastname to generate a good display-name. Others may use individual fields, but core has no replacement for this and I guess should not have. People have different needs here.

I'm just adding a reference to a caching issue here as there seems to be an important bug in core.

hass’s picture

If someone removes it it becomes a contrib blocker.

hass’s picture

mile23’s picture

Version: 8.0.x-dev » 8.1.x-dev

At this point, this should be considered for 8.1.x since it's an API change. #2572821: Fix documentation on hook_user_format_name_alter() should be considered a documentation-only patch for 8.0.x.

@hass: Check out the link in the issue summary, #206 and #199. @znerol mentions that he has alternatives in mind. https://www.drupal.org/node/2112679#comment-10356635

hass’s picture

Version: 8.1.x-dev » 9.x-dev

This is 9.x now as it would otherwise break realname once 8.1 comes out. And that is not acceptable.

I cannot read any of these alternatives in the linked comment.

People only run one module for altering display-name. So, no conflict.

I have overridden autocomplete of entity references in realname D7. It sounds like a lie that it is not working. Have not yet implemented this in D8, but D7 works perfectly.

geerlingguy’s picture

Note that it seems there are a lot of gotchas with D8's hook_user_format_name_alter(); I seem to remember things more-or-less working in D7 with hook_username_alter(), and that's just not the case with D8.

It would be nice if either we deprecate quickly and offer some other reliable way to alter the username, or we lock it in and clean up edge cases (e.g. username display on the user profile page, etc.) ASAP (re: @hass' comment above about Realname module).

hass’s picture

Need some help with the remaining test failures. Can you join, please?

catch’s picture

Title: Remove hook_user_format_name_alter() » Deprecate hook_user_format_name_alter()
Version: 9.x-dev » 8.3.x-dev

If we're going to remove it in 9.x, we need to deprecate it in 8.x first, so moving back.

hass’s picture

If you deprecate something, what is the name of the new function that alters the display name? The feature is still required and IMPORTANT!

andypost’s picture

Suppose the proper way to render "realname" is special ds field that build from tokens

hass’s picture

This is how realname works, but without the alter hook I cannot change the display name. This makes this case a won't fix.

andypost’s picture

@hass suppose realname very useful so better to refactor it into calculatable field somehow like http://cgit.drupalcode.org/drupal/tree/core/modules/text/src/TextProcess...

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.

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.

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.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank you for creating this issue to improve Drupal.

We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Active

From what I can tell using gitlab search this isn't used much so probably could still be deprecated.

avpaderno’s picture

The issue summary says that allowing modules to change the username with hook_user_format_name_alter() has a lot of down-sides and not a lot of up-sides but does not make clear which those down-sides are. At least, the issue summary should explain what those down-sides are.

The documentation for the hook says it can be used to ensure user privacy in situations where $account->getDisplayName() is too revealing. I think that could still be a use case for the hook.