4

I have two Rules (created with the Rules module) that are both launched when an article is flagged with the Flag Approved:

  • Rule 1. Publish the flagged article with the action Publish content.
  • Rule 2. Count the amount of published articles by the author of the flagged article. Display a message when this number is 5.

I'm using the following code in the condition of Rule 2:

return MYMODULE_count_articles($flagged_node->uid) == 5;

where my custom module contains the function

function MYMODULE_count_articles($uid) {
  $count = db_query("SELECT COUNT(*) FROM node WHERE uid=$uid AND status=1 AND type='article'")->fetchField();
  return $count;
}

The condition of Rule 2 is evaluated as FALSE when the 5th article is flagged with Approved, but evaluates as TRUE when a 6th article is flagged.

I suspect that this problem is caused because the database hasn't been updated yet when Rule 2 launches, because debugging the $count variable at the 5th flagging returns 4 instead of 5. Also, executing the condition's code afterwards yields the correct value 5.

Is this a know problem/phenomenon or a bug in the Rules module? Or is there an easy way to fix it (apart from changing the condition to ... == 4, which is really dirty)?

2
  • Could it be cache? See here. I'm not sure what Rule 1 is doing to publish the node, but in case if it's not using node_save then that link could contain the answer: Flush the flagged node's cache before getting the count. Maybe in the same function or by doing a separate function call...
    – Beebee
    Sep 10, 2015 at 20:43
  • @Beebee: I've clarified in my question that Rule 1 publishes nodes with a default action Publish content. Adding a second action that evaluates the PHP code from your link didn't solve the problem. Neither did the usage of that code in the condition of Rule 2.
    – Jeroen
    Sep 10, 2015 at 21:22

1 Answer 1

3

Have a look at issue # 430274, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which is not a bug, simply how the Rules module works ...).

A possible compromise to avoid these kind of issues, is to transform what you're trying to do in your Rule 2 in a Rules Component, so that your Rule 1 "schedules the execution" (using the Rules Scheduler submodule) of that Rules Component. For example after only a few seconds or minutes later on (if your cron job runs frequent enough, otherwise it'll be next time cron runs). This compromise will ensure that, at the time the Rules Component is executed, the "Publish content" for sure completed, and your "number" will have been updated as expected.

3
  • I actually forgot about this question and only recently tried out your cool solution: I scheduled Rule 2 5 seconds after the current time. Since Rule 2 should be executed as soon as possible, I would like to somehow force cron to run (it only runs once in an hour for the moment). Is there an easy way to do that or should I ask this in a new question?
    – Jeroen
    Sep 13, 2016 at 23:22
  • @Jeroen : I am not aware of any Rules related solution to force cron to run. Though I'd be interested to learn/find out about that from anybody else. So maybe you should post a new question on that. Until some solution/answer for that comes up, you probably know about these 2 temporary work arounds: (a) run cron more frequently (every X mins) or (b) maybe "Elysia cron" may help somehow on this (not sure about that however). Sep 14, 2016 at 6:32
  • Thanks Pierre. I asked drupal.stackexchange.com/questions/214769/… and I will already have a look at Elysia cron, since that module might be what I am looking for.
    – Jeroen
    Sep 14, 2016 at 9:37

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.