I am trying to do a simple IF condition but i cant! It seems to me that IF always returnes FALSE regardless of what the IF condition is. I tryed this with a simple PHP return TRUE in IF and this doesnt work either!

Comments

Joel MMCC’s picture

Title: THIS MODULE DOESNT WORK! » If condition always returns False
Priority: Critical » Major

As I understand it, “Critical” should only be used for Bug Reports that result in actual data loss or completely break a site or major functionality thereof. Also, “… DOESN’T WORK!!” isn’t an informative Issue Title.

That said, I, too, am having this issue apparently. My use case does a two-stage “If” / “(Else) If” / “Else”. The “(Else) If” and “Else” branches work as expected, but the “If” branch is never executed even when its condition is met. It used to work and then stopped working, so this may have to do with a PHP version upgrade (forced on me by my hosting service, though I tried in a local development server with the previous PHP version with the same results), some other newly installed or upgraded module interfering, etc. I’ll try debugging with XDebug and see what I can figure out.

In some cases where you’re testing against a value for an entity parameter (but not for things like I was doing such as the Count of items in a List), you may be able to replace your If with a logically equivalent Switch Case. That may work for you.

Joel MMCC’s picture

Title: If condition always returns False » Some conditions (List count comparison?) always returns False

Okay, I just did a simple test Rule Component as follows:

{ "rules_test_rules_conditional" : {
    "LABEL" : "Test Rules Conditional",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules_conditional", "rules" ],
    "USES VARIABLES" : { "boool" : { "label" : "True or False?", "type" : "boolean" } },
    "DO" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "data_is" : { "data" : [ "boool" ], "value" : "1" } },
            "DO" : [
              { "drupal_message" : { "message" : "IF branch taken. Value is True." } }
            ]
          },
          { "ELSE" : [
              { "drupal_message" : { "message" : "ELSE branch taken. Value is False." } }
            ]
          }
        ]
      }
    ]
  }
}

I did an Execute, then tried it with both True and False, and it worked as expected.

So, the problem here does not appear to be in If/Else itself, but in the condition being evaluated. Apparently some conditions which should return True are returning False instead.

In my use case, I did a “Fetch entity by property” of a certain type into a List with Limit 2 (since I’m only interested in how many there are [and then only if there are zero, one, or more than one], not anything specific about them, and I figured that limiting it to 2 would execute faster if there were more than two). I followed that with a Conditional If on the “List count comparison” comparing it with 1. This is followed by an (Else) If on whether the “Data value is empty” which handles the case of zero items (I used that instead of doing “List count comparison” on 0 because I suspect that the problem is there), followed by a simple Else to handle the case where the list count is 2 (or more in the original Entity list).

This used to work, but now fails, and I don’t know why. Where there’s only one item in the list of entities, the If branch still drops through to the (Else) If, which of course doesn’t execute since the list isn’t empty, and on through to the Else. If there are zero items in the list of entities, the If doesn’t execute (as it should not in this case) and falls through to the (Else) If, which does execute just fine and does the action requested, so that works okay. If more than one item in the list, the Else branch executes as it should, but as I said, it also executes if there’s just one as it should not.

So the fault apparently lies in whatever code executes “List count comparison,” since only the branch using that fails to evaluate correctly. I may try changing the order of these to verify.

This also doesn’t explain the original reporter’s claim that If fails even when evaluating a PHP expression of True or False, though I’m not sure how that was even done since I don’t see a way to enter PHP expressions directly into Rules.