4

I'm trying to check if a date field value has been changed on a node update. (Rules Drupal 7)

Here's my rule configuration:

    { "rules_node_update" : {
    "LABEL" : "Update",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave--expedient" : { "bundle" : "expedient" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node-unchanged" ], "field" : "field_tria" } },
      { "NOT data_is" : {
          "data" : [ "node-unchanged:field-tria" ],
          "value" : [ "node:field-tria" ]
        }
      }
    ],
    "DO" : [ { "drupal_message" : { "message" : "UPDATED DATE" } } ]
  }
}

Event: Before saving (i have already tried with node update)

NOT data comparison Parameter: Data to compare: [node:field-tria], Data value: [node-unchanged:field-tria]

What i'am trying to do here is... IF the (new) date value IS NOT EQUAL to unchanged field value (old)... THEN do something. (like print message on screen)

The result of the data comparison returns always TRUE and don't know why.

Notes:

  • Having proven the suggested changes, the result remains the same. I edit a node, and whether I change the value of the "tria" field or not, it still returns true.
  • If I (temporary) OMIT the data comparison it shows the final message. Here's an example of what i'm getting with my setup: Edit a node of this content type, the node already has a date data in the "tria" field. Without making any changes, just save. It shows the message. If i change that field value to another and then save. It also shows the message. So, i think node-unchanged value must me empty because she's not doing the data comparison the right way.
  • Here is the debug of the variables after saving/updating a content : Debug value Parameter: Value to debug: [node-unchanged:field-tria] ... (Array, 1 element) 0 (Array, 6 elements) value (String, 19 characters ) 2001-01-10 01:00:00 value2 (String, 19 characters ) 2001-01-10 01:00:00 Debug value Parameter: Value to debug: [node:field-tria] ... (Array, 1 element) 0 (Array, 10 elements) value (String, 19 characters ) 2001-01-20 01:00:00 value2 (String, 19 characters ) 2001-01-20 01:00:00 Values are there, and they are different! as expected!
  • The rule evaluation log shows this once the node has been updated with different values:enter image description here

The rule evaluation log shows this once the node has been updated with no changes on the values... just saved enter image description here

5
  • Can you confirm (also) that this field is NOT a multi field value? FYI: debugging is the art of taking away all possible causes why something does wor as you'd expect it to work ... PS: since you don't have the "informed" badge yet, I hope you don't mind I say so, but I wonder if you are aware of this Mar 24, 2017 at 10:38
  • 1
    Yes! @Piere.Vriens it IS a multi value field... (Thank you so much for your patience, i'm new here) Mar 24, 2017 at 10:48
  • Merci for adding these debugging logs (and the +1 ...!). Terribly tough formatting for this kind of remote debugging, but better then nothing. Merci for that "multi field value or not" comment, ,THAT explains what is about sure your problem! Give me some time to go update my answer soon (15 to 30 mins or so, go hav a coffee (or cerveza?) while waiting ...) Mar 24, 2017 at 10:50
  • @Piere.Vriens I have removed my field comparison and added this php condition, and it works! $day1 = $node->field_tria['und'][0]['value']; $day2 = $node_unchanged->field_tria['und'][0]['value']; if ($day1 != $day2) { return true; } Mar 24, 2017 at 11:31
  • I just see your prior comment (after I saved my updated answer). Great news (for you) you got it to work. But the bad news (IMO) is that you'd have to store PHP code in your database (that is bad practise). However, by digesting your code, It seems like very helpful as additional clarification that should have been included in your question. Because with that, I "could" get it to work also using the list-processing as detailed in my answer. Though I think my current answer is "as good as it gets for now" (and did help you to find 'a' solution, no?) Mar 24, 2017 at 11:41

2 Answers 2

4

Part 1 - Just to be sure

Add an extra Rules condition like "entity has field", related to that field-tria, before your existing Rule Condition where you do that data comparison.

If that doesn't seem to help, then (temporary) remove the data comparison, to make sure your message is shown for those scenarios as in your Rules Event / Condition (that the entity has that field). If you even don't get the message in this case, then something is really wrong with your rule (eg: you'd be using the wrong Rules Event).

Part 2 - Rules debugging needed

If the above doesn't help, I suggest you start some Rules debugging, as detailed in "How can I display the value of a variable inside a condition?", to get more details about why your rule doesn't seem to work as you'd want it to.

Part 3 - Why your data comparison cannot work

As per your additional comment "this is a multi value field": THAT pretty much explains why your rule seems to behave bizarre ... But sorry, it's rather that you're trying to make Rules do something (compare 2 values), without providing Rules all the info it needs.

If I was to show you 10 glasses of beer (each with their own label say), and I also show you another glass and ask you "is this other one the same?", then the predictable question that would come up is "as compared to which of those 10 glasses?". So that's similar to what's happening in your current rule.

How to move forward

Your Rules Event is pretty sure OK, but you're missing a crucial Rules Action: a Rules Loop. If you're not familiar with such loops, then I strongly recommend to have a look at the tutorial titled "Lists and loops".

In your case, you should add a loop related to that list of dates (which you confirmed to be a multi value field). After you do, you should iterate over each item in that list (= each date). And for each iteration you should then perform the "equivalent of the Rules Condition" you are trying to get to work, whereas you should be using the list item being processed in the current iteration step).

To be honest, I'm afraid that the above will get you closer to the complete solution, but the remaining challenge appears to be "how to specify the date value (a single one) that is to be compared with each of those date-list-items?". Correct me if I'm wrong, but your question is still not clear on that.

PS: this answer (cearly) relates to D7.

4
  • Thanks for your reply. Having proven the suggested changes, the result remains the same. I edit a node, and whether I change the value of the "tria" field or not, it still returns true. I have included the code of the rule in the original post in order to analyze it better. Mar 24, 2017 at 8:49
  • @Piere.Vriens OMITTING the comparison, Yes it shows the final message. Here's an example of what i'm getting with my setup: Edit a node of this content type, the node already has a date data in the "tria" field. Without making any changes, just save. It shows the message. If i change that field value to another and then save. It also shows the message. So, i think node-unchanged value must me empty because she's not doing the data comparison the right way. Mar 24, 2017 at 9:10
  • @CarlosSolé Well, if I was you, I would start the "debugging" as I suggested, that way you can be sure instead of the "I think" in the last phrase of your previous comment. I also start to think of something else that might be causing it, but before going that route, I'd first need to be sure if what you"think" is also "reality". Only rules debugging can tell us... Mar 24, 2017 at 9:24
  • Hi @Piere.Vriens, here is the debug of the variables after saving/updating a content : Debug value Parameter: Value to debug: [node-unchanged:field-tria] ... (Array, 1 element) 0 (Array, 6 elements) value (String, 19 characters ) 2001-01-10 01:00:00 value2 (String, 19 characters ) 2001-01-10 01:00:00 Debug value Parameter: Value to debug: [node:field-tria] ... (Array, 1 element) 0 (Array, 10 elements) value (String, 19 characters ) 2001-01-20 01:00:00 value2 (String, 19 characters ) 2001-01-20 01:00:00 Values are there, and they are different! as expected! Mar 24, 2017 at 10:06
2

The problem here was that the date field was a multi value field. So the data comparison does not work. Instead of using that function, (after much debugging. Thanks to @Piere.Vriens) I have chosen to create my own validator in php and it is simple like this:

if(isset($node->field_tria['und'])){
$day1 = $node->field_tria['und'][0]['value'];
$day2 = $node_unchanged->field_tria['und'][0]['value'];
if ($day1 != $day2) {
return true;
}
}
4
  • Good for you Carlos that you do have a solution you are happy with. If I was asked to evaluate (approve/reject) it, I'd say "ok, this works, but it requires PHP code to be stored in my DB, which is a no-go to me. Instead, reverse engineer the code that seems to solve your question in more accurate specifications, and implement that to further complete the list-processing based solution as detailed in my own answer". PS: some day I might post a self answered question to further complete the prototype in y answer, in which your PHP-code logic is supported also (I'll let you know when I'm done). Mar 24, 2017 at 11:52
  • Despite my previous no-go comment, +1 for posting your own solution ALSO (it'll help me some day to create a rules sample that does the very same thing, with zero custm code stored in the DB). Mar 24, 2017 at 11:54
  • 1
    I have not been able to find a question similar to mine, so I have decided to do it here. It is very productive to get answers as clear and well explained as yours. Easy to learn lot this way! Thank you. I hope I can see that solution without code that you explain. Maybe a video on youtube? Mar 24, 2017 at 12:01
  • Merci for the kudos. I'm happy that dispite your challenging question, with (quite) some comments / suggestions, I was able to make you understand the core of your actual Rules problem (it also took me 24 hours, and actually thought about the multi-value-field possibility during the walk with my dog this morning ... About video: a much more effect explanation, IMO, is an answer in which I include an export of a rule that you can import in your own site late on (youtube does not support such copy/paste I think). Stay tuned (though probably not today ...). Mar 24, 2017 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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