Hello.

I am using hook_node_presave in my module. How can I stop the node saving process and execute intead this some custom coode using this hook.

Thanks.

Comments

bander2’s picture

You can execute your code inside your implementation of hook_node_presave:

function MYMODULE_node_presave($node){
  // Your code here will execute
}

Are you saying you don't want to save the node?

- Brendan

Spider84’s picture

Yes.
I want to check some condition and execute some code WITHOUT saving the node(I will do some very customized node saving).
I have found one way to stop the saving process - to make a redirect.
But may be there is a more correct way?

bander2’s picture

There's always a lot of ways to do things in Drupal. I would go about it by altering the node edit form with hook_form_alter and have it submit to my own function instead of the standard:

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'page_node_form') {
    // I would look at what's in $form['#submit'] before just 
    // overwriting it like this there might be a validation 
    // function or something in there you want to keep
    $form['#submit'] = array('MYMODULE_submission_handler');
  }
}

function MYMODULE_submission_handler($form, &$form_state){
  // Do my custom PHP stuff
}

This way you are not trying to stop the save process which is already in progress, but you are stopping it before it starts. You'll also be dealing with just the form values and not a fully assembled node which might be easier or harder for you to work with depending.

- Brendan

Spider84’s picture

Thank you very much! It is just what I need.

Spider84’s picture

Sorry for writing in this topic again. Can I see the code of default Submit handler?

blacklabel_tom’s picture

In your hook print the contents of:

$form['#submit']

This will give you a list of the submit handlers on the form. Searching for those functions will show you all the code that is run by default.

Cheers

Tom