You are here

Drupal User Login Logout Messages Module

UPDATE (August 30, 2016): The module is now called User Login Logout Messages, which displays a message when users log in as well as log out. It works flawlessly on Mediography.by, my media tech startup. I'm so jazzed! :D

UPDATE (August 29, 2016): Thanks to users @Barami & @joekers below, I was inspired to eventually create a custom module (because we're never supposed to hack Drupal core if at all possible). Most of the tough code was taken from a discussion thread in Drupal. Feel free to visit my sandbox project page to test it out. Thanks again guys, I did it! ;)

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.

And for those of you that use the Real Name module (which is compatible with LoginToboggan & Persistent Login), try the following code that I actually think looks best, whether you decide to include the @realname in the message or not:

/**
* 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();
}

Again, just change the message in red to customize it to however you want it. That's it! :D

You are here