Note: This has an accepted bounty, but no accepted answer because I still want to know how to add a node save confirm dialog using drupal code, not javascript. If you can write up a good answer on that I'll definitely accept it, and probably open and award you another bounty for your trouble.
Trying to implement part of the functionality mentioned in How can I add or remove links to nodes in a list depending on the values of some of the fields I would like to give users a warning, if the node they are submitting would end up being a stub.
I've been trying to implement this by following lots of examples related to confirm_form, especially this one.
I'm think my issue must be that I'm starting from hook_form_alter, so in hook_form_alter it doesn't work very well to return an entirely different form (the confirm_form). Realizing that hook_form_alter must be the wrong starting place though, doesn't help me know where I should start.
So here's what I need to be able to do:
When the user clicks 'Save' on a node edit form, I need to then evaluate whether the field values meet some criteria, and then provide the user a warning, that saving in the current state will have some effect on the way the node is displayed and give them an option to confirm or cancel.
Here is one of the many iterations of what I've tried:
function MYMODULE_form_node_form_alter(&$form, &$form_state, $form_id) {
if (in_array(strstr($form_id, '_', TRUE), $GLOBALS['conf']['publication_types'])) { // only work on the publication forms
if( !isset($form_state['storage']['submitted'] )) { // if the form hasn't previously been submitted
$form['actions']['submit']['#submit'] = array('MYMODULE_node_form_submit');
return $form;
} else { // this is the confirmation step
$form = _MYMODULE_build_confirm_form($form_state);
return $form;
}
}
}
/**
* invoke hook_node_validate
*/
function MYMODULE_node_validate($node, $form, &$form_state) {
if (MYMODULE_is_stub($node)) {
$form_state['storage']['stub'] = True;
if ($form_state['triggering_element']['#value'] === 'Preview') {
// dpm($form_state);
drupal_set_message($message = 'If saved in this form, your publication will not have enough information to merit its own publication page.', $type = 'warning', $repeat = TRUE);
}
} else {
$form_state['storage']['stub'] = False;
}
}
function MYMODULE_node_form_submit($form, &$form_state) {
if( !isset($form_state['storage']['submitted'] )) // this is the first time this has been submitted
{
// not confirmed yet.
// $form_state['storage']['confirm'] = TRUE; // this will cause the form to be rebuilt, entering the confirm part of the form
$form_state['storage']['submitted'] = TRUE;
$form_state['rebuild'] = TRUE;
}
else // if we get here, confirm should have been clicked on the confirm form
{
node_form_submit($form, $form_state);
}
}
function _MYMODULE_build_confirm_form(&$form_state) {
$path = current_path();
$form['processed'] = array(
'#type' => 'hidden',
'#value' => 'true'
);
return confirm_form($form, t('Do you really want to save a publication stub?'), $path, 'Confirm', 'Cancel');
}
This code doesn't work. When you click save gets through returning the form created by _MYMODULE_build_confirm_form, but in the processing afterwards, it seems clear that the system is expecting a node edit form and doesn't like what it is getting instead. The confirmation form is never displayed. I've tried using the node edit form as an input into confirm_form and the actions of the node edit form show up instead of the confirm_form buttons.
I'm at the end of my rope, how do I do this? I guess I need to override the menus so that I can run a callback function to determine which form should be displayed at node/%id%/edit before it gets to the stage of altering the form that has been specified? If so, how can I do that? I just need a solution that will use my existing MYMODULE_is_stub() feature to determine whether there needs to be a confirmation form, and then display the warning to the use and ask them to confirm or cancel. Please help!