3

Is there a difference between using AND in Rules Conditions and simply adding Rules Conditions one after the other?

3

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

Correction to the prior answer

I'm sorry, but the prior answer is only partially correct (which is the same as saying partially wrong, n'est-çe pas). Please allow me to briefly correct that (before anybody believes it):

  • The question is about (contains) "... using AND in rules conditions ...", while the Conditional Rules module cannot be used in Rules Conditions (only in Rules Actions). Actually Rules Actions are the reason-to-exist for this (interesting) module.
  • If you want to do something more complex, then you can ALSO use Rules Components as an alternative. So "You can only do that with conditional rules" is not true.

To AND and/or to OR?

Moving on now to the actual question, this is my answer to it:

  • This question is a typical Rules newbie question. Since it isn't obvious that within the Rules Conditions there are 3 ways to combine conditions:

    • add an explicit "OR", so that either condition must be true (for the Rules Actions to be executed).
    • add an explicit "AND", so that both conditions must be true (for the Rules Actions to be executed).
    • do not add an "OR" or an "AND": that's equivalent to an explicit "AND".
  • One might ask then So when do I ever really need to add an "AND"? The answer to that is if you need to more complex logic like in this sample:

    Condition 1 OR (Condition 2 AND Condition 3)

    In plain English: the Rules Actions will be triggered if either (or both) of these conditions are true:

    • Condition 1.
    • Condition 2 AND Condition 3.

Rules Conditions inside Rules Actions

But, the real Rules fun starts when this (typical) question arises: "I need to add something like a Rules Condition inside the Rules Actions, how can I do that?" ( * ).

To resolve this typical question, there are basically 2 approaches (that I am aware of):

  • Use the Conditional Rules module. For an example using the Conditional Rules 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?".
  • Rework the relevant part of your Rules Action(s) into performing a Rules Component. Because in a Rules Component, it is possible to also add Rules Conditions.

    Typical scenario: "Use Rules to perform a redirect for users with at least X user points, after some Rules Event Y happened". To get this to work:

    • Create a Rules Component, with number of userpoints as a parameter. Within this Rules Component, add a Rules Condition (there you are!) to check if the parameter is at least X and add a Rules Action to perform the redirect.
    • Create a rule, triggered when Rules Event Y happened. Retrieve the nr of user points for the current user (How to???). And add a Rules Action to invoke the previous Rules Component.

Sweet, no?

( * ): There are dozens of samples that are variations of this question, my favorite one is when some Rules Action needs to happen based on the number of User Points the current user has (and you do not like to use PHP as a Rules Condition, like I do). If you don't see why that's a variation of it, then first try to find out what the Rules Condition for that would be ... hint: it's a Rules Action, not a Rules Condition.

  • 1
    Really extensive and exhaustive answer, thanks a lot! I finally had to combine AND and OR, so I complete understood the using case! – Ponzio Pilato Feb 9 '17 at 16:50
  • 1
    @PonzioPilato : merci! ... and sorry I could not let the prior answer just go. Thought I'd try to explain this typical question for "once and forever". FYI: when I first started with Rules, I struggled with the same thing over and over (now I know by heart). – Pierre.Vriens Feb 9 '17 at 16:54
  • 1
    Oooooooooh yes. – No Sssweat Feb 9 '17 at 16:56
  • 1
    I'm completely agree! Quite often we, as Drupal newbies, need some clear and simple answers to simple questions that are missing of documentation and waste so much our time. Thanks again :) – Ponzio Pilato Feb 9 '17 at 17:00
  • Some people understand things with only a few words (or just reading the source code) ... other people prefer a tutorial to get it. – Pierre.Vriens Feb 9 '17 at 17:05
3

No difference, except if you want to do something more complex like this:

____ AND _____ OR _____ AND _____

You can only do that with conditional rules. (If I recall correctly)

  • Sorry to say, but part of this answer is not correct (= wrong). Lucky you ... I rarely downvote answers (and for sure not anonymously). Do you mind if I allowed to correct this with an additional answer? ... So that maybe next time you want to recall it you have something to go search for? – Pierre.Vriens Feb 9 '17 at 15:28
  • @Pierre.Vriens please feel free to do so :) – No Sssweat Feb 9 '17 at 15:32
  • merci for the approval (hm, hm) .. but don't blame me afterwards if your answer would get un-accepted hé ...! Stay tuned ... – Pierre.Vriens Feb 9 '17 at 15:34
  • @Pierre.Vriens I will be happy to delete it, if I am completely off. – No Sssweat Feb 9 '17 at 15:44
  • No no, no need for delete, it's only partially wrong! Hang on (I'm working on it ... go have a coffee if you're in a hurry/too curious). Or go checkout the newest "promo ad"-proposal on meta.drupal.SE – Pierre.Vriens Feb 9 '17 at 15:46

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.