3

Scenario: When users click the "log out" link, I'd like a confirmation form to pop up asking them to click again "yes" to confirm and continue logging out.

Is there module that does this? Or some kind of Rules workflow?

2 Answers 2

2

Use jQuery

Pop-up

(function ($, Drupal, window, document, undefined) {

    $(document).ready(function(){
        $('a[href="/user/logout"]').click(function(e){
            if(!confirm("Are you sure you want to log out?")) { e.preventDefault(); }
        });
    });

})(jQuery, Drupal, this, this.document);

Or text replace

(function ($, Drupal, window, document, undefined) {

    $(document).ready(function(){
        $('a[href="/user/logout"]').click(function() {
            $(this).removeClass('confirm');
            $(this).text("Are you sure?");
            $(this).unbind();
            return false;
        });
    });

})(jQuery, Drupal, this, this.document);

Simple Middle Page

One very simple way to accomplish this would be to create a new page with a simple confirmation message.

For example: Are you sure you want to <a href='user/logout'>log out</a>?

Then link to your new page in whichever menu the log out link belongs.


Rules Link

There is a Rules Link module that can create links with JavaScript confirmations, as well as links that use confirm_form() You could automatically attach a log out link with confirmation to the User entity.


The l() Function

You may also be able to implement the l() function to generate the link you need.

$logout_link = l("Logout", "user/logout", array( 'onclick' => 'return confirm( "Are you sure?" ); ' ) );

Form API

You could use the Form API with a custom submit handler/redirect.

$form['logout'] = array(
    '#type' => 'submit',
    '#value' => t('Logout'),
    '#attributes' => array('onclick' => 'if(!confirm("Really Logout?")){return false;}'),
);
1
  • Great suggestions, thank you for the helpful list! Commented Jul 27, 2012 at 15:03
1

Depending on how custom you want it, a simple JavaScript popup could work just fine:

$('a.logout-link').click(function(e){
  var result = confirm("Are you sure you want to logout?");
  if(!result){
    // Stop the link from working.
    e.preventDefault();
  }
});

If you wanted to do something more custom, you could create your own popup and style with CSS. Not sure what your programming level is with Drupal.. But the JavaScript is simple enough :)

2
  • Does the log out link need the logout-link class applied first?
    – Citricguy
    Commented Jul 27, 2012 at 3:17
  • 1
    The jQuery selector you use should be based on what class, ID, or other unique selector the element has. api.jquery.com/category/selectors Commented Jul 27, 2012 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.