Cannot implement a custom user cancellation method

Created on 11 May 2020, over 4 years ago
Updated 20 March 2024, 6 months ago

Problem/Motivation

Currently we pretend that we have an API that allows a 3rd party to implement its own user cancellation method. Relevant hooks: hook_user_cancel_methods_alter() and hook_user_cancel().

However, if you create a custom method, let's say user_cancel_archive, the code for user_cancel_block method is also executed. It's easy to find why, by looking to _user_cancel() function. The default: case is the problem. But for my custom method I want to be able to set my own status message and send my own custom email. No luck, Drupal is settings its status message and sends the standard user block email. Also, it's not possible to exclude _user_cancel() from the batch operations.

I think that this bug was possible because we have no test for a custom user cancellation method. I've added a test to prove the problem.

Proposed resolution

Based on @alexpott comment/proposal from #14:

  • Deprecate the actual hook_user_cancel() hook.
  • Create a new event (AccountCancelEvent) and move the logic from hook_user_cancel() implementations in event subscribers.
  • Make sure the user event subscriber, which actually perform the user cancellation, runs last.
  • A third-party that needs to fully replace the default core's cancellation logic, is able to implement a subscriber with a higher priority, do its logic and stop the event propagation, so that downstream subscribers won't execute.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

Third-party that need to have a say on user account cancellation, should subscribe to \Drupal\user\Event\AccountCancelEvent event. A subscriber aiming to implement its own user account cancellation logic and avoid default core behavior should set a higher priority and bypass downstream subscribers by stopping the event propagation.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
User moduleΒ  β†’

Last updated 1 day ago

Created by

πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

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.

Production build 0.71.5 2024