Score:0

Messenger doesn't display value after user logged out and redirect

kn flag

After my user is submitting a form, I want to logged him out, then redirect to the front page. then, display a message.

public function submitForm(array &$form, FormStateInterface $form_state) {
  /**
   * my logic
   **/
  if ($isDeleted) {
    $this->messenger()->addMessage('Logged out');
    user_logout();
    $home = \Drupal\Core\Url::fromRoute('<front>')->setAbsolute();
    $form_state->setRedirectUrl($home);
  }
}

This code works fine, but the message doesn't show up. I read/assumed that the messenger is linked to the current session, so by log out the user, the messenger is destroyed.

So I tried to create another messenger after the user logout, but still, the message isn't displayed.

unusedspoon avatar
aq flag
Might seem obvious but is the "Status messages" block definitely set to show on your homepage? Do other messages display on your homepage ok?
kn flag
Yes, if I remove the "user_logout()" function, the message works (so the block works)
unusedspoon avatar
aq flag
Yes you're probably right the messages are stored via the session which is cleared on logout. Have you tried switching the position in the code of the user_logout() and the message setting? A possible alternative is you redirect to the homepage with a url parameter e.g. ?user_logged_out and then use some JS or something to create/show a message
kn flag
yes, I tried to switch the code, no changes. I'd like to avoid to add parameters to the url (at the last resort, I'll probably do it)
Jaypan avatar
de flag
https://www.drupal.org/forum/support/module-development-and-code-questions/2009-06-15/show-you-are-now-signed-out-message#comment-12483191
imclean avatar
my flag
There's a core issue related to this. https://www.drupal.org/project/drupal/issues/754560
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.