TransWikia.com

How to prevent a node being saved when using the Rules module?

Drupal Answers Asked by nielsv on October 26, 2021

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?

4 Answers

You could hide the "save" button with jQuery based on the settings of the field that you want to check against with a simple display:none; CSS setting AND Use Page Manager to show a message in a panel, also with a simple display:block; It's not really waterproof but most users wouldn't go fiddling with the CSS. And together with the "unpublish" solution offered by Yuri, that should solve your issue.

Answered by BassPlaya on October 26, 2021

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.

Answered by Yuri on October 26, 2021

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.

Answered by Yuri on October 26, 2021

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

Answered by Pierre.Vriens on October 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP