Switch to hook_user_presave() for setting username

Created on 10 January 2018, almost 7 years ago
Updated 13 October 2023, about 1 year ago

Problem/Motivation

Triggering a save inside either of the post save hooks (hook_ENTITY_TYPE_insert() or hook_ENTITY_TYPE_update()) is a dangerous thing to do, as you potentially end up in horrible recursive scenarios. The documentation for both state:

* This hook runs once the entity has been stored. Note that hook
* implementations may not alter the stored entity data.

The impact of this is more limited for hook_ENTITY_TYPE_insert() as it itself will not run the second time and $entity->original will not exist the first time round, so will be correctly loaded. But even with insert, you can still end up in situations where a module which runs later may have an update hook run before an insert hook (for example) and other data issues.

Proposed resolution

Switch to hook_user_presave() to set the username. The only draw back to this is that the ID is not available for implementations of hook_email_registration_name(). I think stability is more important than that one use case, and modules that are desperate to do that could always do that manually themselves.

Remaining tasks

Make the switch...

API changes

$account->id() would not be available in hook_email_registration_name().

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom andrewbelcher

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany Anybody Porta Westfalica

    I think this needs further maintainer feedback to decide, how to proceed here? Or from someone very very experienced in Drupal development? Any active maintainers?

    Or could someone raise this in slack perhaps for help?

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    3 pass, 2 fail
  • @anybody opened merge request.
  • 🇩🇪Germany Anybody Porta Westfalica

    Created MR!10 from #20 by @JeremySkinner - please note for credits.

    So we can proceed and review here more easily.

  • Assigned to Grevil
  • 🇩🇪Germany Anybody Porta Westfalica

    @Grevil could you please have a look, now, as you're deep into this area?
    So we can probably finish this! What do you think? We might create a 2.x branch for switching to semver and have this possibly dangerous change separated for now?

  • Status changed to Needs work about 1 year ago
  • 🇩🇪Germany Grevil

    We might create a 2.x branch for switching to semver and have this possibly dangerous change separated for now?

    The current MR's changes are the NON-dangerous compatibility changes. We might as well merge them as a current workaround and create a follow-up issue / leave this issue open.

    Although I agree with @krystalcode here. The hook implementations wasn't properly implemented in the first place. And I do not see a case, where we do not have the $account variable in presave?

    So I'd say we should rather orientate our code on patch #18 by @krystalcode rather than #20. And adjust our submodule logic accordingly.

  • 🇩🇪Germany Anybody Porta Westfalica

    #20 would be no danger, while I'd say we should be careful with #18 as it changes functionality and in contrast to the current implementation means, the username will change, when the user is being resaved! That might be unexpected for existing sites.

    I'm fine with a MR here and I'm fine to plan this for a 2.x release, as it's kind of breaking change. But we shouldn't rush here?

  • 🇩🇪Germany Anybody Porta Westfalica

    PS: Implementing #18 will take things closer to the logic of the new submodule. I think the logic from there (when to change an existing username) is good and should be uplifted then @Grevil?

    Would you like to prepare a MR from all of that, once the other issues are fixed?
    Targeting 2.x then (in the MR name?)

  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Grevil

    Yea, let's do that!

    the username will change, when the user is being resaved!

    Yea, we need to circumvent that, similar to how we did it in the submodule.

  • Issue was unassigned.
  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024