Drupal - How can I add a cancel button to the content add form for a single content type?
I would use the following code.
function mymodule_form_my_image_node_form_alter(&$form, &$form_state) {
// If the node is being created, redirect the users to the page where
// they can choose the content type for a new node; otherwise, redirect
// them to the page showing the node.
$destination = (!isset($node->nid) || isset($node->is_new)) ? 'node/add' : "node/{$node->nid}";
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), $destination),
'#weight' => 20,
);
}
Notes
markup is the default form element type; there is no need to use
#type = 'markup'
.In the node edit form, all the buttons are inside
$form['actions']
. The weights used from the buttons that form is using are 5 (Save), 10 (Preview), and 15 (Delete). I used 20 to show the link after those buttons, but you can change its weight to show it in a different position.// Add the buttons. $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || (!form_get_errors() && isset($form_state['node_preview'])), '#value' => t('Save'), '#weight' => 5, '#submit' => array('node_form_submit'), ); $form['actions']['preview'] = array( '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_DISABLED, '#type' => 'submit', '#value' => t('Preview'), '#weight' => 10, '#submit' => array('node_form_build_preview'), ); if (!empty($node->nid) && node_access('delete', $node)) { $form['actions']['delete'] = array( '#type' => 'submit', '#value' => t('Delete'), '#weight' => 15, '#submit' => array('node_form_delete_submit'), ); }
There isn't the need to use
arg()
to understand if the form is for editing an existing node, or for creating a new one. I checked!isset($node->nid) || isset($node->is_new)
isTRUE
, to verify the node is being created. Drupal seems to simply checkempty($node->nid)
, even though node_object_prepare() uses!isset($node->nid) || isset($node->is_new)
.If
$_GET['destination']
is set, the link added withl()
will point to that path.
Each node form has its own unique id. It's consisted of node type's machine name followed by _node_form
. So your my_image
node type's form id is my_image_node_form
Here's a mock untested function (D6) to illustrate how to implement:
function HOOK_form_alter(&$form, &$form_state, $form_id) {
$types = array(
'my_image',
);
foreach($types as $type) {
if($type.'_node_form' == $form_id) {
if(arg(1) == 'add') {
// If adding node link to node/add screen
$link = l(t('Cancel'), 'node/add');
}
elseif(arg(2) == 'edit') {
// If editing node, link to node view screen
$link = l(t('Cancel'), 'node/'.arg(1));
}
$form['cancel'] = array(
'#type' => 'markup',
'#value' => $link,
'#weight' => 0,
);
}
}
}
If using Drupal 7 then change #value
on the array to #markup
.
All those solutions are for adding Cancel link not a button!
If you want to add the button instead of a link you should use this code for module cancel_button.module
:
function cancel_button_form_alter(&$form, &$form_state, $form_id) {
// node types for which Cancel button should be added
$types = array(
'biography',
'faq',
'draftlaw',
'vocabulary',
'draftlaw_page',
'encyclopedia',
'user', // if you want to add user's edit form too
);
foreach($types as $type) {
if($type.'_node_form' == $form_id || $type.'_profile_form' == $form_id) {
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#access' => TRUE,
'#weight' => 55,
'#submit' => array('cancel_button_form_cancel', 'node_form_submit_build_node'),
'#limit_validation_errors' => array(),
);
}
}
}
/**
* cancel_button callback.
*/
function cancel_button_form_cancel($form, &$form_state) {
$link = '';
$path = 'node';
if('user_profile_form' == $form['#form_id']){
$path = 'user';
}
if(arg(1) == 'add') {
// If adding entity link to [user|node]/add screen
$link = $path . '/add';
}
elseif(arg(2) == 'edit') {
// If editing entity, link to node view screen
$link = $path . '/' . arg(1);
}
$url = $_GET['destination'] ? $_GET['destination'] : $link;
drupal_goto($url);
}