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.

  1. Add a new rule

    Add new visibility rule to panel pane. Select PHP Code option when configuring new panel pane visibility rule.

  2. 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>');

    Temporarily output contexts variable as Drupal message.

    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.


  3. 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);

    PHP code returns a boolean value to Panels visibility rule check

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

mr.oobov’s picture

I've spent the whole day trying to solve my problem, and the answer was here. Thank you for this.

yaelsho’s picture

Thanks!

Yael

brylie’s picture

This example was very helpful. I was trying to learn how to access node attributes, specifically "promoted". This helped me down the path :-)

bennash’s picture

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.

Funksmaname’s picture

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:

if ($contexts['argument_entity_id:node_1']->data->nid == $contexts['context_entity:node_1']->data->field_related_event['und'][0]['target_id']) {
return TRUE;
} else {
return FALSE;
}

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.

smartasc’s picture

Awesome! Thanks!

museumboy’s picture

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:

return empty($contexts['panelizer']->data->field_replacement_banner_image['und'][0]['fid']);

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

coreteamvn’s picture

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...

Jitujain’s picture

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.