Based on the snippet from Pascal Duez:
https://gist.github.com/pascalduez/1888373 (thanks a lot!) I modified a bit, here's a snippet to add a "Cancel" button in Content (Node) create / edit forms.
Simply use your own mini module to implement it.
/**
* Custom cancel button callback.
*/
function mymodule_form_cancel($form, &$form_state) {
$fallback_destinaton = '
// 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);
}
?>
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if (stristr($form_id, '_node_form') !== FALSE) {
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#access' => TRUE,
'#weight' => 999,
'#submit' => array('mymodule_form_cancel', 'node_form_submit_build_node'),
'#limit_validation_errors' => array(),
'#attributes' => array('class' => array('btn-cancel')),
);
}
}
?>
If the "Cancel" button is clicked, the user is being redirected the following way:
- If a "destination" paramter is set, this is used
- If no destination parameter is set and we're in an edit form, the user is being redirected to the node display
- Otherwise the user is being redirected to the frontpage