Drupal 6 multiple submit buttons
There is a bit more to adding multiple submit buttons in Drupal 6 than at first glance. I didn't find an article that covered all the elements I needed, so here is a condensed version.
So this is how I ended up achieving multiple buttons that reference the node ID.
1) You need to add the buttons first, this is fairly easy and there are a number of tutorials about this online. Basically this is how you do that.
In the form_alter function of your Drupal module you can add lines like this to add new buttons. You can alter the text on the default button using the #value attribute.
// This is a the default save button
$form['buttons']['submit']['#value'] = 'Default Save'; // Change text on the button
// This is a new custom save button
$form['buttons']['button2']['#type'] = 'submit'; // Type of the button, Drupal will do its magic
$form['buttons']['button2']['#value'] = 'Save Only'; // Change text on the button
$form['buttons']['button2']['#weight'] = 7; // order of the buttons.
2) Create a function that Drupal will auto-magically call in your module.
function yourmodule_node_form_submit($formID, &$form_state) {
if($form_state['clicked_button']['#value'] == $form_state['values']['submit']) {
$_SESSION['yourmodule_default_button_clicked'] = 'true';
unset($_SESSION['yourmodule_save_only_button_clicked']);
} else if($form_state['clicked_button']['#value'] == $form_state['values']['save_only']) {
$_SESSION['yourmodule_save_only_button_clicked'] = 'true';
unset($_SESSION['yourmodule_default_button_clicked']);
node_form_submit($form, &$form_state); // Only call for custom buttons. If called for default button will save twice.
}
}
3) Build nodeapi hook.
yourmodule_yournode_nodeapi is not called auto-magically, you need to call this from yourmodule_nodeapi, something like this:
function yourmodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (isset($node->type)) {
switch ($node->type) {
case 'yournode':
yourmodule_yournode_nodeapi(&$node, $op, $a3, $a4);
break;
}
}
So then write the nodeapi code.
function yourmodule_yournode_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
// This is called before insert.
if ($op == 'presave') {
if ( isset($node->nid) ) {
// This is called when the node is updated
if (isset($_SESSION['yourmodule_default_button_clicked'])) {
$_REQUEST['destination'] = 'SomeAwesomePage?nid=' . $node->nid;
} else if (isset($_SESSION['yourmodule_save_only_button_clicked'])) {
$_REQUEST['destination'] = 'node/' . $node->nid . '/edit';
}
}
} else if ($op == 'insert') {
// This is called when a new node has just been created.
if (isset($_SESSION['yourmodule_default_button_clicked'])) {
$_REQUEST['destination'] = 'SomeAwesomePage?nid=' . $node->nid;
} else if (isset($_SESSION['yourmodule_save_only_button_clicked'])) {
$_REQUEST['destination'] = 'node/' . $node->nid . '/edit';
}
}
}
Final notes:
- It is not obvious at first that the default submit button will call node_form_submit auto-magically. That means you must not call node_form_submit for the default submit button, or you will get the node saved twice.
- If you want to reference the node ID then you must do this in nodeapi, it can be tricky to link the form save to the nodeapi, the use of session variables was made here, but perhaps there is a better way, please leave a comment below if you know of a better way to achieve this link.
References:
http://drupal.org/node/34123
http://naveensnayak.wordpress.com/2010/03/09/drupal-multiple-submit-buttons/
Comments