User menu

Home » Blog 63 » Display message to users on logout in drupal

Display message to users on logout in drupal

Whenever you are implementing login functionality in Drupal, you will come across this issue where you want to display confirmation message to users upon logout. There is a drupal API which get called upon logout, but you can never use 'drupal_set_message()' there. I dont know exact reason behind this, but it seems like drupal is destroying something when logging out and this request to set message will never get passed. So, as an alternative i have found following way to achieve this functionality(In your custom module, add following codes):

/**
 * Implements hook_user_logout() 
** 
*/
function moduleName_user_logout($account){
  user_cookie_save(array('logoff' => 1));
}

/**
 * Implements hook_block_info().
** Create custom blocks 
*/
function moduleName_block_info() {
  $blocks = array();
  
  $blocks['logout'] = array(
    'info' => t('moduleName : Logout Message'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;  
}

/**
 * Implements hook_block_view() 
**
** Write output for block 
 */
function moduleName_block_view($block_name = ''){
  switch ($block_name){
    case 'logout':    
      if(isset($_COOKIE['Drupal_visitor_logoff'])){    
        if($_COOKIE['Drupal_visitor_logoff'] == 1 && !user_is_logged_in()){
          drupal_set_message(t('You have successfully logged out. For security reasons, exit your web browser.'));
          user_cookie_save(array('logoff' => 0));
        }
      }
      $block = array(
        'subject' => t('Logout'),
        'content' => '',
      );
      break;
  } 
}