I have a Drupal 7.38 site where a "Content is viewed" rule will not fire if Drupal Cache is enabled, and a cached page is served to the user. The rule fires when Drupal needs to build the page before serving it, like if the currently cached page has expired, an authenticated user requests the page, or cache is disabled.
To try and figure out what was happening, I set Rules to "Log all warnings and errors", to "Log debug information to the system log", and to show debug information "Always". I cleared the cache, and refreshed a node page as an anonymous user. When I went to "Recent log messages" there was a log entry like this:
Rules debug information:
" Reacting on event Content is viewed.
- 0 ms Reacting on event Content is viewed.
- 16.007 ms Evaluating conditions of rule Increase radioactivity on view. [edit]
- 18.186 ms The condition data_is evaluated to TRUE [edit]
- 18.205 ms AND evaluated to TRUE.
- " Rule Increase radioactivity on view fires. [edit]
- 0 ms Rule Increase radioactivity on view fires.
- 0.327 ms Evaluating the action radioactivity_emit. [edit]
- 47.705 ms Rule Increase radioactivity on view has fired.
- 66.037 ms Evaluating conditions of rule Notify about popular article. [edit]
- 134.984 ms The condition data_is evaluated to FALSE [edit]
- 135.008 ms AND evaluated to FALSE.
- 135.055 ms Finished reacting on event Content is viewed.
However, subsequent views of the page as an anonymous user did not create more "Content is viewed" log entries, and the action for the rule did not fire. Refreshing the page as an authenticated user did create log entries. If I cleared the cache, or waited for the cache to expire, and then refreshed the page as an anonymous user, a log entry was made only for the first refresh of that page.
I have two separate rules for this type of event:
- Events - Content is viewed of type Article
- Conditions - Data comparison - Parameter: Data to compare: [view-mode], Data value: Full content
- Actions - Add an incident to a field - Parameter: Radioactivity field: [node:field-weekly-popularity], Energy amount: 10
The second one is a bit complicated, so I would need to attach a screen capture of it. I can do that if someone thinks it would be useful to look at it. However, I don't think the problem is with how the rules are set up, since they work fine when cache is disabled.
I don't know whether to file this as a support request or bug report. I have a dev site with the same Drupal/Rules version, and most of the same modules installed. It doesn't have this issue, so my guess is that there is a conflict with another module, or I have overlooked a (probably obvious) setting somewhere.
Comments
Comment #1
ben greenberg commentedComment #2
ben greenberg commentededit: Changing category to "Support request"
Comment #3
tr commentedYes, you are correct that "Content is viewed" "...fires when Drupal needs to build the page before serving it". Specifically, "Content is viewed" is fired whenever hook_node_view() is called, and that only happens with Drupal builds/renders the page. If you're caching the built pages using any caching mechanism, then hook_node_view() will not be called every time a page is served, only when the page needs to be rebuilt.
If you want to trigger on every time the page is served, you will have to trigger on something else like the path of the page.