Sep
09
2013

Drupal 6 - Creating a simple module to confirm logout

The idea is to create a simple drupal module which takes over the default logout path and putting a confirmation form.

logoutconfirm.info:

; $Id$
name = Logout confirm
description = To confirm logging out of user.
package = Drupal custom modules
core = 6.x

 

logoutconfirm.module:

<?php
//$Id$

/**
 * @file
 * Use this module to confirm user logging out.
 */

/**
 * Implementation of hook_menu_alter().
 */
function logoutconfirm_menu_alter(&$items) {
 
// Overriding the default logout path
 
$items['logout']['page callback'] = 'logoutconfirm_page';
  unset(
$items['logout']['file']);
}
/**
 * Page callback.
 */
function logoutconfirm_page() {
 
$output = t('Are you sure you wish to logout?');
 
$output = $output.drupal_get_form('logoutconfirm_form'); // Adding confirmation form
 
return $output;
}
/**
 * Confirmation form
 */
function logoutconfirm_form($form_state) {
 
// Define confirmation yes/no buttons.
 
$form['logoutconfirm']['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Yes'),
  );
 
$form['logoutconfirm']['no'] = array(
    
'#type' => 'submit',
    
'#value' => t('No'),
  );
  return
$form;
}
/**
 * Handle submission of the form.
 */
function logoutconfirm_form_submit($form, $form_state) {
 
// Store the value of clicked button in a variable.
 
$form_values = $form_state['clicked_button']['#value'];
  if (
$form_values == 'Yes') {
    global
$user;

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

   
// Destroy the current session:
   
session_destroy();
   
// Only variables can be passed by reference workaround.
   
$null = NULL;
   
user_module_invoke('logout', $null, $user);

   
// Load the anonymous user
   
$user = drupal_anonymous_user();

   
drupal_goto();
  }
  elseif (
$form_values == 'No') {
   
drupal_goto();
  }
}
?>