Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal User Logout Message Module

Parent Feed: 

I recently noticed in Drupal that although there are modules that let you display user login messages such as "Log in successful for @username." (LoginToboggan) or "Welcome back, @username." (Persistent Login), there's no easy way to display user logout messages.

So after doing some research, I found a patch for Drupal 7 that I just updated to work on the recently released Drupal 7.50. Let's take a quick look at the simple code changes in the modules/user/user.pages.inc file (lines 194 - 214):

1) Before:

/**
* Menu callback; logs the current user out, and redirects to the home page.
*/
function user_logout() {
user_logout_current_user();
drupal_goto();
}

/**
* Logs the current user out.
*/
function user_logout_current_user() {
global $user;

watchdog('user', 'Session closed for %name.', array('%name' => $user->name));

module_invoke_all('user_logout', $user);

// Destroy the current session, and reset $user to the anonymous user.
session_destroy();
}

2) After:

/**
* Menu callback; logs the current user out, and redirects to the home page.
*/
function user_logout($message = 'You are now logged out.', $status = 'status') {
global $user;

watchdog('user', 'Session closed for %name.', array('%name' => $user->name));

module_invoke_all('user_logout', $user);

// Destroy the current session, and reset $user to the anonymous user.
session_destroy();

// Sends a message to the user.
if (!empty($message)) {
drupal_set_message($message, $status);
}

drupal_goto();
}

As you can see, the code change is pretty much self-explanatory. Just change the message in red to customize it to however you want it.

So there you have it. I tested the code out a few times and it works flawlessly.

P.S. Does anyone know how to display the username in the user logout message, such as "You are now logged out, @username."? Let me know in the comments, and I'll give you credit. Thanks! :)

UPDATE (August 28, 2016): Thanks to user @Barami below, I was inspired to solve the problem in my own simple way. Here is the new code:

/**
* Menu callback; logs the current user out, and redirects to the home page.
*/
function user_logout() {
global $user;

watchdog('user', 'Session closed for %name.', array('%name' => $user->name));

$message = "You are now logged out, <em>$user->name</em>.";

module_invoke_all('user_logout', $user);

// Destroy the current session, and reset $user to the anonymous user.
session_destroy();

drupal_set_message($message, 'status');

drupal_goto();
}

And I think this code actually looks better than the original solution, whether you decide to include the @username in the message or not. Just change the message in red to customize it to however you want it.

UPDATE (August 29, 2016): It turns out that I needed to tweak my code one more time, because I use the Real Name module (which is compatible with LoginToboggan & Persistent Login). For those of you who also use it, here is the final code:

/**
* Menu callback; logs the current user out, and redirects to the home page.
*/
function user_logout() {
global $user;

watchdog('user', 'Session closed for %name.', array('%name' => $user->name));

$realname = format_username($user);
$message = "You are now logged out, <em>$realname</em>.";

module_invoke_all('user_logout', $user);

// Destroy the current session, and reset $user to the anonymous user.
session_destroy();

drupal_set_message($message, 'status');

drupal_goto();
}

And yes, I think this code actually looks best, whether you decide to include the @realname in the message or not. Just change the message in red to customize it to however you want it. That's it! :D

UPDATE (August 29, 2016): Thanks to users @Barami & @joekers below, I was inspired to eventually create a working custom module (which I originally couldn't get to do exactly what I wanted). Most of the code was taken from a discussion thread in Drupal. Feel free to download and use it (I also created a sandbox project page, which I will update shortly), because we're never supposed to hack Drupal core if at all possible. Thanks again guys, I did it! ;)

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web