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..
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();
}
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();
}
}
Comments
Comment #1
dejavu_007 commentedYou can pop up a confirm dialog box using jquery code. Add jquery using drupal_add_js in template.php.
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..
Comment #2
joachim commentedWouldn't be hard to have a custom module take over the /logout path and put a confirmation form at that path.
Comment #3
dejavu_007 commentedI 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 :
loginconfirm.module :
Comment #4
joachim commentedI 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.
Comment #5
tanu487 commentedHey thnks :) will implement this and report asap..
Comment #6
tanu487 commentedHey dejavu_007,Thanks a tonne for the help! It works wonders for me..thanks..
Comment #7
tanu487 commentedComment #8
dejavu_007 commentedThanks joachim, for all the help.
Here are the changes made and has used a single submit handler. I hope this what you meant.
Comment #9
Partha Sarathi commentedHi this is nice for D6. Can anyone suggest the same solution for D7? Thanks for advance.