Panels 3: Pane Visibility Rules (Evaluating $contexts Variable)

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

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.

Help improve this page

Page status: Not set

You can: