I want the user to confirm his logging out action by displaying a warning. Has this feature been implemented? Kindly help.

Comments

dejavu_007’s picture

You can pop up a confirm dialog box using jquery code. Add jquery using drupal_add_js in template.php.

/**
 * When logging out a confirmation dialog box asking "Are you sure you wish to logout?" appears.
 */
drupal_add_js(
 '$(document).ready(function(){
   $("a[href*=\'/logout\']").click(function(){
	if(confirm("Are you sure you wish to logout?")){
		return true;
	}
	else{
		return false;
	}
   });
  });', 'inline'
);

I am not sure if this is the right way to do it.
Anybody, please post if there is any modules that can do it or if there is a better way to do it.
Thanks..

joachim’s picture

Priority: Critical » Normal
Issue tags: -warning, -log out

Wouldn't be hard to have a custom module take over the /logout path and put a confirmation form at that path.

dejavu_007’s picture

I am new to drupal, so I am not sure whether this module is right. Please correct me if it is wrong.
Here is how i made a custom module.
loginconfirm.info :

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

loginconfirm.module :

<?php
//$Id$

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

/**
 * Implementation of hook_menu_alter().
 */
function logoutconfirm_menu_alter(&$items) {
	$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');
	return $output;
}
function logoutconfirm_form($form_state) {
	// Define a submit function.
	$form['logoutconfirm']['submit'] = array(
	  '#type' => 'submit',
	  '#value' => t('Yes'),
	);
	$form['logoutconfirm']['no'] = array(
	  '#type' => 'submit',
	  '#value' => t('No'),
	  '#submit' => array('no_logout'),
	);
	return $form;
}
/**
 * Handle submission of the form.
 */
function logoutconfirm_form_submit($form, $form_state) {
	drupal_set_message(t('The form has been submitted.'));
	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();
}
function no_logout($form, $form_state) {
	drupal_goto();
}
joachim’s picture

I would use a single submit handler for both buttons, ie don't set #submit, and look in $form_values to see which button was clicked.

But apart from that, looks fine. Very good for a newbie, in fact! :D

You can create a sandbox project for that code if you like, btw.

tanu487’s picture

Hey thnks :) will implement this and report asap..

tanu487’s picture

Hey dejavu_007,Thanks a tonne for the help! It works wonders for me..thanks..

tanu487’s picture

Status: Active » Closed (fixed)
dejavu_007’s picture

Thanks joachim, for all the help.
Here are the changes made and has used a single submit handler. I hope this what you meant.

function logoutconfirm_form($form_state) {
	// Define a submit function.
	$form['logoutconfirm']['submit'] = array(
	  '#type' => 'submit',
	  '#value' => t('Yes'),
	);
	$form['logoutconfirm']['no'] = array(
	  '#type' => 'submit',
	  '#value' => t('No'),
	);
	return $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();
}
}
Partha Sarathi’s picture

Issue summary: View changes

Hi this is nice for D6. Can anyone suggest the same solution for D7? Thanks for advance.