6

Is it possible to prevent a node from being saved with Rules? This is what I do now:

  • Create a rule Before saving content of type ''.

    • Condition: IF ...
      • TRUE => Redirect & set message
  • Create a rule After saving content of type ''.

    • Calculate & set value

So when a condition is true before saving I want to redirect the user, show a message and DON'T save node.

When it's not true I want to save the node and do the actions in the after saving content of type rule.

The problem is that when the condition is true in the first rule, the redirect and set message is performed. But directly afterwards the node is saved and the actions in the other rule are executed.

How can I make sure that the second rule doesn't get executed? Can I do this without custom code?

  • Perhaps the 'rules_conditional' module could help you here – scott Oct 19 '15 at 7:48
2

I would try to use the Conditional Rules module for this. Some more details about this module (from its project page):

... provides Rules plugins for conditionally altering execution flow directly inside a group of actions.

So create a rule in which you add these actions:

  • Conditional (which you add via the 'Add conditional' link at the bottom)

  • If Condition (which you add via the 'Add If' link to the right of the 'Conditional' action, and 'Condition' is what you mentioned in your first rule already).

  • Redirect & set message (which you add via the 'Add Action' link to the right of the 'If' action, and 'Redirect & set message' is what you mentioned in your first rule already).

  • Else (which you add via the 'Add Else' link to the right of the 'Conditional' action.

  • Calculate & set value (which you add via the 'Add Action' link to the right of the 'Else' action, and 'Calculate & set value' is what you mentioned in your second rule already).

Here is a screenprint of a rule with an action that uses Conditional Rules (though only with an "if"-part here, no "else"):

enter image description here

  • I tested your rule but even after forcing the redirect, the node is still saved. – Yuri May 6 '16 at 11:56
  • An option is to delete the node immediately after it is saved and then redirect to either the node creation page or another page. Form input is lost of course in that case. – Yuri May 6 '16 at 11:59
  • @Yuri: you're right, the node is still saved. However, IMO a more elegant option (alternative) as compared to a delete, is to "unpublish" such node. Also, by adding drupal.org/project/rules_forms it should be possible to stop the node from being saved (I might post an updated answer about that soon). – Pierre.Vriens Jun 28 '16 at 16:37
0

You can use node form validation with the Rules Forms Support module.This will allow a node not to be saved when performing rules actions.

0

An easy solution that I use myself (no need for additional modules or php code), is to unpublish the node when the condition is not met. In that way, saving the node is not an issue, and the form data is still available in the unpublished node. Then redirect to the unpublished node and show the message.

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.