4

Is there a way to nest Rules, or make them behave more like an if / then / else functionality.

Currently I can only figure out how to trigger a Rules Action based on a Rules Condition, but not how to choose between 2 Rules Actions.

In addition, is there a way to call one rule from another?

  • The obvious way would be to use custom php in your rule, but that pretty much defeats the entire purpose of Rules. I would like to know the best way to do this as well. Fantastic question @user379468 – Citricguy Feb 1 '12 at 14:04
3

Your question consists of multiple questions, so my answer also consists of multiple parts ...

Part 1

To implement conditional logic in Rules, your very first attempt should be to only use Rules Conditions, and try to use the AND and OR operators to combine multiple Rules Conditions. If no such operator is specified, then the default is that ALL Rules Conditions must be true for the Rules Actions to be executed (this is equivalent to using the AND operator).

For way more details about this, refer to my answer to "What's the difference between combining Rules conditions with AND and just have them follow one after the other?".

Attention, even though using these AND and OR operators may seem obvious, it is NOT! For an example of how confusing it can be, refer to the video tutorial about "Combining conditions".

Part 2

Currently I can only figure out how to trigger a Rules Action based on a Rules Condition, but not how to choose between 2 Rules Actions.

You're right, to "choose between Rules Actions" is like performing Rules Conditions within Rules Actions, which you cannot do with only the Rules module. However, the Conditional Rules module allows you to exactly do that (execute Rules Conditions as part of Rules Actions).

My favorite example to illustrate this can be found in my answer to "How to check whether one's current User Points are negative with the Rules module?", which includes a rule that looks like so:

{ "rules_display_userpoints_after_updating_content" : {
    "LABEL" : "Display userpoints after updating content",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "userpoints_rules", "rules", "rules_conditional" ],
    "ON" : { "node_update" : [] },
    "DO" : [
      { "userpoints_rules_get_current_points" : {
          "USING" : { "user" : [ "site:current-user" ], "tid" : "all" },
          "PROVIDE" : { "loaded_points" : { "total_points" : "Number of points in all categories together" } }
        }
      },
      { "drupal_message" : { "message" : "You now have [total-points:value] points" } },
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : { "data" : [ "total-points" ], "op" : "\u003C", "value" : "20" } },
            "DO" : [
              { "drupal_message" : { "message" : "You have sufficient points (you still have [total-points:value] ...)." } }
            ]
          },
          { "ELSE" : [
              { "drupal_message" : { "message" : "You DO NOT have sufficient points (you only have [total-points:value] ...)." } }
            ]
          }
        ]
      }
    ]
  }
}

This rule retrieves, as the very first Rules Action (not Rules Condition!), the current amount of user points of a user. And after the Rules Action to display some message, you can see the Conditional Rules module at work (starting from CONDITIONAL): it will show a message that depends on the total amount of points.

For another example using the Rules Conditional module (for which there is not a lot of docu available), refer to my answer to the question "How to prevent a node being saved when using the Rules module?".

Bonus: Instead of using if/else (conditional) logic within the Rules Action, you can also use Switch logic. For another example using such Switch logic, refer to rule included in my answer to "How to set the values of some fields depending on the value of a list box?".

Be aware: the Conditional Rules module only has a beta release for D7, though it's used in over 8K sites (which makes it seem like a pretty robust module though).

As an alternative to using the Conditional Rules module, you can also create corresponding Rules Components, with similar Rules Actions to be performed, but also with additional Rules Conditions equivalent to any Rules Conditions you'd want to include by using the Conditional Rules module. And then perform that Rules Component inside your Rules Actions of your original rule. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?"

Part 3

In addition, is there a way to call one rule from another?

That's what Rules Components is all about, which you can do for:

  • only Rules Conditions, using a Rules Component of plugin type "Condition set".
  • only Rules Actions, using a Rules Component of plugin type "Action set".
  • both Rules Conditions and Rules Actions, using a Rules Component of plugin type "Rule".

Rules Components may use variables, whereas there are 2 variations of them:

  • Parameters which are used as input that the Rules Component needs in order to execute.
  • Provided which are output (instead of input), and which can be used (after the execution of the Rules Component completed) in the rule that calls the Rules Component.

For more details about this, refer to:

PS: I'm assuming this question is about D7.

  • 1
    Great write up as usual. Personally, I think Rules components are something every Drupal dev should be more capable at. The possibilities are endless if you're willing to chain some together. – Niall Murphy Aug 14 '17 at 17:52
  • 1
    Nice complement from somebody I know, for sure, is a Rules expert, aka @NiallMurphy ... merci! And yes, consider me as the very last one to be convinced about your every Drupal dev statement. But, at the same time D8rules is, to me, the nr 1 showstopper for D8. Therefor I hope that in D9 the same will happen to Rules as what happened to Views in D8 ... R.I.C. aka RulesInCore ... – Pierre.Vriens Aug 14 '17 at 17:59

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.