How to trigger a Rule when content is viewed with Panels activated? Easy problem: when users visit a node page on my website, one of the node's integer fields should be changed. Answer: Rules. Now comes the tricky part...

I installed Panels and enabled the node template node_view via the Pages UI. I added the Pane "Node being viewed" content (build mode: Full content), but none of my Rules with an event Content is viewed is triggered anymore on the node pages. After searching around a whole day on the internet, I think my problem is a classic, but I didn't find a clear answer anywhere. Hopefully, we can get over it here once and for all.

Here are some things that I tried so far:


This comment suggested to disable the No extras option in the Pane's settings (which works), but that also renders the comments, comment form, etc.; which is not what I want, since I add those as separate Panes.
Combining the power of Conditional Rules and Rules URL Argument. The general idea is to check on Drupal is initializing whether the path starts with node, save the nid arg(1) as a variable, load the node and make the necessary changes afterwards. Unfortunately, I couldn't find a way to use the nid in the path without using custom PHP (I want to avoid that).
Forgetting about Rules and trying to code is also fine for me, but I haven't found the correct hook yet. Using hook_node_view doesn't work, because it doesn't get called (that's precisely why the Rules aren't triggered). I also tried to use

function hook_init() {
  if (arg(0) == 'node') {
    $node = node_load(arg(1));
  }
}


in my custom module, but that results in some theme related errors, so I'm afraid that my node templates are getting messed up by this. Lastly, different sources referred to hook_ctools_render_alter, so that seemed promising. Though, after some debugging, I quickly gave up trying to understand how this hook works. If this is the correct way to go, I would like to have some more information about the hook or some example code.


Do you have any suggestions to fix this frustrating problem?

Note: I'm using the currently latest versions of all modules, i.e. CTools 1.11, Panels 3.8 and Rules 2.9.
You should use Rules Bonus Pack which allows to create a new event when a panel variation is loaded.

Detailed instructions:


Download Rules Bonus Pack and enable "Rules Bonus: Miscellaneous" sub module.
From the panel settings page you will see a new "Reactions" tab. On this tab you will select the new option "Create a Rules event for this variant".
Now open the rule and add a new event. You will find a new section "Rules Bonus: Miscellaneous" with the selected panel variation. Use this event instead the "Content is viewed" one.


It should work as you expect.

Alternative solution

An alternative solution is to write a custom module. You should take a look to the Rules Bonus Pack source code. You can find some inspiration to solve the problem.

Instead using hook_node_view() you can implement hook_ctools_render_alter().

First you should check the handler (node_view) and after load the node data from the ctools/panels context.

Example:

function yourcustommodule_ctools_render_alter(&amp;$info, &amp;$page, &amp;$context){
  // Verify if the page/node is the one you want to interact with
  // and set the field value as you wish.

  // First check if the handler is node_view:
  if($context['handler']-&gt;task == "node_view"){

    // Then verify the node content from the context sub array
    // The $context variable contains the node data but you
    // have to dig in.
    $node = $context['contexts'][key($context['contexts'])]-&gt;data;

    // [... your code ...]

}


Happy coding :-)