Messages are lost if set just prior to logout

Created on 27 March 2010, over 14 years ago
Updated 2 March 2023, over 1 year ago

A drupal_set_message() issued during the logout process is lost and never displayed.

What I did: Created an system_message_action() and associated it with the user_logout trigger.

What I expected: A message displayed to the user after logout.

What happened instead: No message was displayed.

I imagine this is because the session is destroyed, and the messages are queued in the session. This is not the expected behavior, though.

Solution for D7 when writing custom code:

      module_load_include('pages.inc', 'user');
      user_logout_current_user();
      drupal_set_message('My message will be shown to the user after they are logged out.');
      drupal_goto();

**Next Steps**
1. Confirm if there's a similar approach for D8/D9/D10.
2. Confirm if higher-level APIs (like the action/trigger API) exhibit this problem. If so, can they be modified using the above technique?

πŸ› Bug report
Status

Active

Version

9.5

Component
User systemΒ  β†’

Last updated 1 minute ago

Created by

πŸ‡ΊπŸ‡ΈUnited States rfay Palisade, CO, USA

Live updates comments and jobs are added and updated live.
  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

  • Needs manual testing

    The change/bugfix cannot be fully demonstrated by automated testing, and thus requires manual testing in a variety of environments.

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.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I think this is doable in D9+ as follows:

    Implement hook_user_logout which gets called before logout, set some sort of flag that a user is being logged out.

    Add an event listener for the \Drupal\Core\Session\AccountEvents::SET_USER event, if the flag from above is set and the new set user is anonymous, add the message, if not, unset the flag.

    Bit janky to have a hook for before and event for after, but the APIs are there.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Should this not be fixed in core?

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Are you meaning that core copies the messages from one session to the next?

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Yes.

  • Status changed to Active over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Ok, then let's keep it open

Production build 0.69.0 2024