Question

When I press the delete button on some content, I'm taken to a confirmation page. The delete option is a button, while the cancel option is a link. That looks pretty weird. I found that there's a form_confirm() function in drupal, but I can't understand how to use it. Does anyone know how to make the cancel link into a button?

Was it helpful?

Solution

Or this using no javascript (and replacing eregi() with preg_match()...

  if ( $form['#theme'] == '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);
      preg_match('/href\s*=\s*\"([^\"]+)\"/', $no, $href);
      $form['actions']['cancel']['#value'] = '';
      $form['href']=array(
        '#type'=>'value',
        '#value'=>$href[1],
      );

      // Add our own button
      $form['actions']['docancel'] = array(
        '#type' => 'submit',
        '#name' => 'cancel',
        '#submit' => array('mymodule_confirm_form_cancel'),
        '#value' => $value,
      );

    }
  }

and

function mymodule_confirm_form_cancel(&$form,&$form_state) {
  $href=$form['href']['#value'];
  if ( !is_null($href) ) {
    $form['#redirect']=$href;
  }
}

OTHER TIPS

The reason the cancel link looks like a link, is because it is a link <a>, while the confirm button, is a form submit element <input type="submut>.

If you want to make the cancel link, to look like a submit button, you can do that with pure CSS.

Using hook_form_alter() , try this :

if($form['#theme'] == '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);
      eregi('m|href\s*=\s*\"([^\"]+)\"|ig', $no, $href);

      $form['actions']['cancel']['#value'] = '';

      // 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 = "'.$href[1].'";',
        ),
      );
      // Prevent the form submission via our button
      $form['#attributes']['onsubmit'] = 'if ($(this).attr("allowSubmission") == "false") return false;';
    }
  }

For Drupal 7 I use:

/**
 * Implements hook_form_alter().
 */
function yourmodule_form_alter(&$form, $form_state, $form_id) {
  // Change 'cancel' link to 'cancel' button. 
  if ( $form['#theme'] == 'confirm_form' ) {
    if ($form['actions']['cancel']['#type'] == 'link') {
      $title = $form['actions']['cancel']['#title'];
      $href = $form['actions']['cancel']['#href'];
      if (!is_null($title) and !is_null($href)) {
        // Disable Cancel link.
        $form['actions']['cancel']['#title'] = '';
        // Add our own Cancel button.
        $form['actions']['docancel'] = array(
          '#type' => 'submit',
          '#name' => 'cancel',
          '#submit' => array('yourmodule_confirm_form_cancel'),
          '#value' => $title,
        );
      }
    }
  }
}

/**
 * Redirect to previous page after confirm form cancel().
 */
function yourmodule_confirm_form_cancel(&$form, &$form_state) {
  $href = $form['actions']['cancel']['#href'];
  if (!is_null($href)) {
    $form_state['redirect'] = $href;
  }
}

Issue also has been reported for Drupal 8, but Drupal core team has no intention to solve the issue in core. See Drupal support request Change confirmation form Cancel link to a button.

Best regards, Baso.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top