Panel panes can be configured to appear when PHP code evaluates TRUE.
For example, a pane displaying the field(s) for a node may only make sense when that field actually has values.
Using the $contexts variable available to content pane visibility settings, one can evaluate such a condition in PHP.
- Add a new rule
- Inspect the $contexts variable
While the PHP code must ultimately return a boolean value, temporarily output the contents of our $contexts variable using drupal_set_message():
drupal_set_message('Contexts:<br /><pre>'.print_r($contexts,1).'</pre>');
Visit your Panels page and you will see the output of the entire $contexts variable -- a variety of nested objects and arrays pertaining to the page.
- Evaluate your condition(s)
After finding the $contexts properties or element(s), return a boolean value (optionally using the negation (NOT) option instead of a PHP operator):
return empty($contexts['argument_entity_id:node_1']->data->field_file);
In this example, the panel pane will only appear when the specified condition is met--when the array defining the node's file_field property (array) is not empty.
Comments
I've spent the whole day
I've spent the whole day trying to solve my problem, and the answer was here. Thank you for this.
Thanks!
Thanks!
Yael
Thank you!
This example was very helpful. I was trying to learn how to access node attributes, specifically "promoted". This helped me down the path :-)
using Panlizer
I'm using Panelizer for a Node view, and this code works well when showing a custom content pane based on a node's field. In this case my field is a "related article" entity reference field on the content type.
if (!(empty($contexts['panelizer']->data->field_related_article['und'][0]['target_id']))) {
return TRUE;
} else {
return FALSE;
}
I'm sure that could be simplified.
Thank you both!
Thank you both!
To add another option, I managed using the fantastic help in this post and comments to come up with a solution that compares the current page nid with that of a referenced nid of a different node for a visibility rule:
The use case is I have a live event node that holds a video and is referenced to an upcoming event node. If they are both the same, the live video (and twitter feed etc) are published on the upcoming event node.
To do this I had to include the following contexts: the live node nid, and the related video from THAT nid. Then compared the current nid to the referenced from live node nid to hide or show the pane.
Just what I needed
Awesome! Thanks!
No Contexts found
I think I'm trying too hard here. I have a panelized node. I want to hide a pane if a specific field is not empty. There are no added contexts but I see "built in Context" which lists all the tokens for the specific node-type.
Setting the php visibility rule to:
Works, but I get an error that $contexts is undefined. It seems there should be a more direct route to this. What am I doing wrong? Thanks
This here is very helpful,
This here is very helpful, though php filter are generally not recommended (e.g http://drupal.stackexchange.com/questions/2509/what-are-the-downsides-of...)
here is a more difficult, but more in line with best practises approach:
https://www.webwash.net/create-custom-visibility-rules-in-panels-using-c...
Thanks,
Thanks,
It is very helpful to me. I invested so much time to debug the thing but this article solved the problem in a few mins.
We can use dpm() to print $Contexts instead of drupal_set_message. I think dpm is very easy to see the printed output.