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?

up vote 6 down vote accepted

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.

  • 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. – No Sssweat Sep 1 '17 at 18:40
  • @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 ...) – Pierre.Vriens Sep 1 '17 at 18:52
  • Merci for your suggestion. But I am afraid it will get closed as a duplicate. – No Sssweat Sep 1 '17 at 19:09
  • 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). – Pierre.Vriens Sep 1 '17 at 19:32

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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