The following code from the "example.module" will help you to add the cancel button to any node form.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function example_form_alter(&$form, &$form_state, $form_id) { | |
// You might want to filter by content type. | |
if ($form_id == 'YOUR_content_type_node_form') { | |
// Add a cancel button. | |
$form['actions']['cancel'] = array( | |
'#type' => 'submit', | |
'#value' => t('Cancel'), | |
'#access' => TRUE, | |
'#weight' => 15, | |
'#submit' => array('example_form_cancel', 'node_form_submit_build_node'), | |
'#limit_validation_errors' => array(), | |
); | |
} | |
} | |
/** | |
* Custom cancel button callback. | |
*/ | |
function example_form_cancel($form, &$form_state) { | |
$url = $_GET['destination'] ? $_GET['destination'] : 'choose/your/path'; | |
drupal_goto($url); | |
} |
Really helpful. Thank you. I an getting
ReplyDeleteNotice: Undefined index: destination in example_form_cancel().
Do you know why??
You can use isset() to avoid the notice, like this:
ReplyDelete$url = isset($_GET['destination']) ? $_GET['destination'] : 'choose/your/path';