0

I want to show a message via drupal_set_message() after logout and redirects to page.

Code:

/*
 * Implements hook_user_logout().
 */
function example_user_logout($account){
  $login_message = 'You have been redirected';
  drupal_set_message(t($login_message));
  $login_redirect = '/mypage';
  example_redirect($login_redirect);
}

/**
 * Redirect path for logout.
 */
function example_redirect($path) {
  $response = new RedirectResponse($path);
  $response->send();
  exit;
}

Tried commenting exit also, but still no luck. Any help would be appreciable.

4
  • Are you able to fix this issue? I am facing similar issue.
    – cissharp
    Dec 29, 2016 at 16:20
  • Yes, but different method. Passing query parameter and displaying message. After displaying message, unsetting the parameter to not shown second time.
    – Ajay Reddy
    Jan 2, 2017 at 5:08
  • Thank you Ajay. And how are you unsetting parameter? Using javascript or server side code? Can you share?
    – cissharp
    Jan 3, 2017 at 2:42
  • First while creating token i am saving in my custom table and deleting after displaying message
    – Ajay Reddy
    Jan 3, 2017 at 12:27

1 Answer 1

4

drupal_set_message() sets the message in $_SESSION. By logging out, you destroy the current session and thus wipe the message you just set.

5
  • ok, is there any way to display message after logout?
    – Ajay Reddy
    Aug 22, 2016 at 11:50
  • 1
    Only way I can think of, is putting a parameter along in the redirect, like 'action=logout' or such, and check for that parameter when loading the page you redirect users to. The main issue here is you can't use sessions for this, so you'll have to find a way around that. My suggestion here is one option, but I'm sure there are more.
    – Raf
    Aug 22, 2016 at 11:53
  • Raf: I can set parameter but I don't want to see the message over and over when user refresh the same page (with argument). Is that possible?
    – cissharp
    Dec 30, 2016 at 13:58
  • Those messages are cleared from $_SESSION the moment they're shown. In other words: unless you set the same message on every page load, it won't show over and over when the user refreshes the same page.
    – Raf
    Dec 30, 2016 at 14:17
  • Drupal 7 solution drupal.org/forum/support/module-development-and-code-questions/… You can do similar with Drupal 8 and routes.
    – alesr
    Apr 26, 2018 at 14:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.