Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 7 - Add a cancel button on node forms
<?php
/**
* Implements hook_form_alter().
*/
function modulename_form_alter(&$form, &$form_state, $form_id) {
// You might want to filter by content type.
if ($form_id == 'ctype_node_form') {
// Add a cancel button.
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#access' => TRUE,
'#weight' => 15,
'#submit' => array('modulename_form_cancel', 'node_form_submit_build_node'),
'#limit_validation_errors' => array(),
);
}
}
/**
* Custom cancel button callback.
*/
function modulename_form_cancel($form, &$form_state) {
$url = $_GET['destination'] ? $_GET['destination'] : 'choose/your/path';
drupal_goto($url);
}
@weekbeforenext

This comment has been minimized.

Copy link

weekbeforenext commented Aug 31, 2012

This was very helpful. Thanks for posting it!

@elalemanyo

This comment has been minimized.

Copy link

elalemanyo commented Mar 3, 2015

Hi,
If you use the node form on a modal pop up the cancel button is giving an ajax error. Did you try something like this?

@elalemanyo

This comment has been minimized.

Copy link

elalemanyo commented Mar 3, 2015

function modulename_form_alter(&$form, &$form_state, $form_id) {
  // You might want to filter by content type.
  if ($form_id == 'ctype_node_form') {
    if (!isset($form_state['ajax'])){
      // Add a cancel button.
      $form['actions']['cancel'] = array(
        '#type'   => 'submit',
        '#value'  => t('Cancel'),
        '#access' => TRUE,
        '#weight' => 15,
        '#submit' => array('modulename_form_cancel', 'node_form_submit_build_node'),
        '#limit_validation_errors' => array(),
      );
    }

    else {
      $form['actions']['cancel'] = array(
        '#weight' => 15,
        '#markup' => l(t('Cancel'), '#',
          array(
            'attributes' => array(
              'class' => 'ctools-close-modal button'
              ),
            'external' => TRUE
          )
        )
      );
    }
  }
}

/**
 * Custom cancel button callback.
 */
function modulename_form_cancell($form, &$form_state) {
  $fallback_destinaton = 'choose/your/path';
  // If edit, use the node itself as fallback.
  if (!empty($form['#node'])) {
    if (!empty($form['#node']->nid)) {
      $nid                 = $form['#node']->nid;
      $node                = node_load($nid);
      $node_uri            = node_uri($node);
      $fallback_destinaton = $node_uri['path'];
    }
  }
  // Go to destination or fallback.
  $url = isset($_GET['destination']) ? $_GET['destination'] : $fallback_destinaton;
  drupal_goto($url);
}
@8obby6igita1

This comment has been minimized.

Copy link

8obby6igita1 commented Aug 8, 2015

Would it be possible for me to repurpose your code to be able to place a button next to a field in content form that I've created?
Essentially, I'd like the button to be able to generate a random MD5 hash value and place it in the adjacent text field? Thanks!

dS.

@omkargodse

This comment has been minimized.

Copy link

omkargodse commented Sep 18, 2018

Thanks a ton !!!! That was very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.