By Pierre.Vriens


2017-09-01 13:48:35 8 Comments

The Conditional Rules module allows for including Rules Conditions within Rules Actions (using its If/Else constructs), something you cannot do by only using the Rules module. Here are just a few (out of many) typical questions that can be solved by using Conditional Rules:

However, there are some issues with the Conditional Rules module, such as:

  • It does not have an official release for D7 yet.
  • There is not a lot of documentation about it (and no Community documentation).
  • There are some scenarios for which this module produces incorrect results.

Is there any alternative solution for building equivalent Rules logic, and which doesn't require using the Conditional Rules module?

1 comments

@Pierre.Vriens 2017-09-01 13:52:58

For any custom rule where you are tempted to use the Conditional Rules module, you can always 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.

Consider this prototype-example (just assume it's a simplified version of a more complicated rule with preceding Rules Actions like "Fetch entity by ID", "Set a variable", etc):

{ "rules_show_a_message_using_conditional_rules" : {
    "LABEL" : "Show a message using Conditional Rules",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules_conditional", "rules" ],
    "ON" : { "node_delete--page" : { "bundle" : "page" } },
    "DO" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : { "data" : [ "node:promote" ], "value" : "1" } },
            "DO" : [
              { "drupal_message" : { "message" : "Bingo ... using \u0022Conditional Rules\u0022 ..." } }
            ]
          }
        ]
      }
    ]
  }
}

Here is its equivalent using Rules Components:

{ "rules_show_a_message_using_a_rules_component" : {
    "LABEL" : "Show a message using a Rules Component",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_delete--page" : { "bundle" : "page" } },
    "DO" : [ { "component_rules_say_bingo" : { "current_node" : [ "node" ] } } ]
  }
}

And the related Rules Component looks like so:

{ "rules_say_bingo" : {
    "LABEL" : "Say bingo",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "current_node" : { "label" : "Current node", "type" : "node" } },
    "IF" : [
      { "NOT data_is" : { "data" : [ "current-node:promote" ], "value" : "1" } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Bingo ... using a Rules Component instead of \u0022Conditional Rules\u0022 ..." } }
    ]
  }
}

Some details about the alternative using a Rules Component:

  • The Rules Condition within the Conditional Rules construct, has become a Rules Condition of the Rules Component.
  • The actual Rules Action to be performed, was moved to a Rules Action of the Rules Component.
  • The original rule simply calls the Rules Component, thereby passing the node to be processed as a parameter of the Rules Component.
  • The Rules Event in the original rule is unchanged, and if there would have been any additional Rules Conditions in the original rule, those would have remained unchanged also.

Note: the above is just a "concept", to illustrate the key-part of the solution. Because this prototype did not require any previous "Fetch entity by ID" (or "Set a variable", or "load current user points") as a previous action.

@No Sssweat 2017-09-01 18:40:55

Nice! you should also tag this as 8 as I bet it's going to a good while until the Conditonal Rules module gets ported to D8.

@Pierre.Vriens 2017-09-01 18:52:00

@NoSssweat good point, I had not thought of it that way. However, as you may know, the topic of using version tags on Drupal.SE may damage your (what SE calls) reputation. With your D8 expertise/experience, how about you clone this question into a D8 equivalent, someting like "while waiting for conditional rules release for D8, how else can I do this", and hopefully also add your D8 answer to it, possibly by just using a D8 version of my answer (no credits due ...)

@No Sssweat 2017-09-01 19:09:08

Merci for your suggestion. But I am afraid it will get closed as a duplicate.

@Pierre.Vriens 2017-09-01 19:32:14

Poor @NoSssweat being afraid, really? Sorry I'm scaring you! 2nd try: you post a D8 related answer, probably similar to my D7 answer, with a preface something like "If this question was asked in the context of a D8 site, for which there even does not exist such Conditional Rules module, then a similar answer would look like so and so" ... And don't tell me it's not allowed to answer a question with another Drupal release in mind (wouldn't it be fun to start answering all D8 questions with a D7 answer also??? Similar to those D7 questions that received D8 answers already).

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How to use an user profile custom field using Rules?

  • 2018-03-07 17:10:13
  • Toki
  • 903 View
  • 1 Score
  • 2 Answer
  • Tags:   8 rules

1 Answered Questions

1 Answered Questions

[SOLVED] How to perform a page redirect after submitting node with conditions?

1 Answered Questions

[SOLVED] How to automatically assign a role based on email address domain?

  • 2017-02-13 09:00:26
  • TBJ
  • 111 View
  • 2 Score
  • 1 Answer
  • Tags:   7 rules hooks

2 Answered Questions

[SOLVED] How to automatically add new users to a OG Group (MyCommunity) using Rules?

  • 2014-05-27 13:04:43
  • drupal_stuff_alter
  • 1062 View
  • 1 Score
  • 2 Answer
  • Tags:   rules organic-groups

1 Answered Questions

Deduct 10% transaction fee when user donates userpoints to another user using rules - Drupal 7

  • 2014-04-08 06:18:43
  • IT Pro Manager
  • 170 View
  • 3 Score
  • 1 Answer
  • Tags:   7 rules userpoints

1 Answered Questions

[SOLVED] How to build an Email Diggest using Rules

  • 2012-06-17 07:44:37
  • Marc
  • 1421 View
  • 2 Score
  • 1 Answer
  • Tags:   7 rules

1 Answered Questions

[SOLVED] Node not being saved when using Rules redirect

  • 2012-06-22 23:54:50
  • Azz Kawa
  • 903 View
  • 2 Score
  • 1 Answer
  • Tags:   7 rules

Sponsored Content