1

Could you clarify me, how can I prevent node creation if I use hook_node_submit ?

What should I do in code of the hook for it ?
Do returning false ? Or throw an exception ?

That is, if some condition is true then the node simply does not create, without output of any error message.

  • 1
    It doesn't look like there is a way to prevent the node creation in this hook. – Jaypan Jan 21 '17 at 10:39
2

To stop a node being created, you need to implement hook_node_validate instead, and raise an error. The hook is specifically to:

Perform node validation before a node is created or updated.

Which is what you want.

The example in the docs page is probably quite close to what you'll need:

function hook_node_validate($node, $form, &$form_state) {
  if (isset($node->end) && isset($node->start)) {
    if ($node->start > $node->end) {
      form_set_error('time', t('An event may not end before it starts.'));
    }
  }
}

For the record, hook_node_submit is both conceptually the wrong place to try to stop a node being created, and too late in the process to do so anyway. The node hasn't been saved by that point, but you've lost any simple opportunity to stop the remaining submit handlers running and the default process completing. Even if you found a way to short out that functionality, there would be no sensible method to feed that back to the user.

You can throw an exception or kill the page request of course, yes, but Drupal obviously won't be able to recover from that and the request will terminate.

|improve this answer|||||
  • Can I do it without function form_set_error('') ? – Yakimkin Roman Jan 21 '17 at 20:17
  • No, that's what stops the form from submitting and subsequently the node being saved (you can use form_error instead but you have to use one of them). You might get better answers if you edit the question and explain what you're actually trying to achieve, and why invalidating the form, which would be the standard thing to do, isn't desirable – Clive Jan 21 '17 at 20:24
  • Hi @Clive your answer is always great but here I have one question is it possible to achieve this without showing the error message or is it useful if I do not show the error message. In question in the last line it is mentioned without output of any error message. – DRUPWAY Jan 25 '17 at 11:49
0

I don't 'think you can prevent node creation using hook_node_submit. Once this function execute it create node.

You can modify or something like that once node created depend on your conditions.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.