Converting cancel link to a button in confirmation pages

Last modified: January 13, 2008 - 12:17

The following code snippet will allow you to have all "Cancel" links in confirmation screens converted to buttons without touching the core.

Note: This snippet requires jQuery to be loaded.

Simply place this code snippet in a hook_form_alter:

function mymodule_form_alter($form_id, &$form) {
  // Converting all links to form buttons
  if ($form['#base'] = 'confirm_form') {
    $no = $form['actions']['cancel']['#value'];
    if (!is_null($no)) {
      // Get the text to put on the cancel button
      $value = preg_replace('/(<\/?)(\w+)([^>]*>)/e', '', $no);
      // Hide the old cancel link
      $form['actions']['cancel']['#prefix'] = '';
      $form['actions']['cancel']['#suffix'] = '';
      // Add our own button
      $form['actions']['docancel'] = array(
        '#type' => 'button',
        '#button_type' => 'reset',
        '#name' => 'cancel',
        '#submit' => 'false',
        '#value' => $value,
        '#attributes' => array(
           'onclick' => '$(this).parents("form").attr("allowSubmission", "false"); 
                window.location = $("div#'. md5($no) .'").find("a").attr("href");'),
      );
      // Prevent the form submission via our button
      $form['#attributes']['onsubmit'] = 
              'if ($(this).attr("allowSubmission") == "false") return false;';
    }
  }
}

Suggestions and enhancements are welcome.

First of all thanks for this

MaciejSchuttkowski - September 4, 2008 - 15:04

First of all thanks for this usefull tip.
it didn't work for me, but I think I got the idea behind this ;)

It seemed, that there was a javascript-Problem with finding the href of the cancel-link,
(e.g. on "node/123/delete" the form-location on pressing the cancel-Button was "node/123/undefined")
so I modified it a bit with a little regexp-trick so it works for me right now.
The cancel-link was also visible, so I just resetted the '#value' of the cancel-link.
EDIT: The if-equals was missing an equal-Sign too:
if ($form['#base'] == 'confirm_form')

Would be glad if someone could give me some feedback if this was just a
problem that appeared on my Drupal-Installation (5.10) or if it's more widely spread.

So here is my "fix":

<?php
function mymodule_form_alter($form_id, &$form) {
 
// Converting all links to form buttons
 
if ($form['#base'] == 'confirm_form') {
   
$no = $form['actions']['cancel']['#value'];
    if (!
is_null($no)) {
     
// Get the text to put on the cancel button
     
$value = preg_replace('/(<\/?)(\w+)([^>]*>)/e', '', $no);
//+ ADDED:
     
eregi('m|href\s*=\s*\"([^\"]+)\"|ig', $no, $href);
//+ get the value of the "href" from the cancel-link
//-+ REPLACED:
     
$form['actions']['cancel']['#value'] = '';
//-+ Hide the old cancel link
      // Add our own button
     
$form['actions']['docancel'] = array(
       
'#type' => 'button',
       
'#button_type' => 'reset',
       
'#name' => 'cancel',
       
'#submit' => 'false',
       
'#value' => $value,
       
'#attributes' => array(
//-+ REPLACED:
         
'onclick' => '$(this).parents("form").attr("allowSubmission", "false");
                window.location = "'
.$href[1].'";'),
//-+ just print the first result of the eregi-function
//(as in the cancel-link there always be min. and max. 1 instance of ' href="" ')
     
);
     
// Prevent the form submission via our button
     
$form['#attributes']['onsubmit'] =
       
'if ($(this).attr("allowSubmission") == "false") return false;';
    }
  }
}
?>

- - - - - - - - - - - - - - - - - - - - - - -
best regards from a docMatic newbie.

It Worked for me

whan - August 25, 2009 - 04:49

It worked for me, but had to replace if ($form['#base'] == 'confirm_form') with if ($form['#base'] = 'confirm_form')

 
 

Drupal is a registered trademark of Dries Buytaert.