I'm a front end guy who is trying to learn some php but I can't seem to figure this out. I have a select list field in a node called Project Status and I would like to control visibility of various panel regions based on the value of this field. The $context variable is %node:field-proj-status and the php variable is $node->field_proj_status (array) but everything I try returns an error. Thanks in advance

Comments

blackclover’s picture

Thanks to merlinofchaos!

$value = ctools_context_keyword_substitute('%node:field-proj-status', array(), $contexts);
return ($value == 'Approved');

Returns TRUE when the node being viewed proj_status field is set to Approved, exposing the content.

Hopefully this help.

blackclover’s picture

Status: Active » Closed (fixed)
valante’s picture

I've run into this issue myself. I dislike the solution above because it calls an external function when the PHP selection rule screen clearly states you have a $contexts variable available.

I entered the following code in the PHP rule:

drupal_set_message('<pre>' . print_r($contexts, true) . '</pre>');

Then I saved, loaded a sample node, read the message, and got the correct index for the field I wanted to test. Voila!

merlinofchaos’s picture

Title: PHP code snippet for configuring a Panels Visibility Rule based on a select list field value. » Document: PHP code snippet for configuring a Panels Visibility Rule based on a select list field value.
Project: Panels » Chaos Tool Suite (ctools)
Version: 7.x-3.x-dev » 7.x-1.x-dev
Component: Panel pages » Documentation
Assigned: Unassigned » esmerel
Status: Closed (fixed) » Active

This should be part of the documentation.

blackclover’s picture

Sorry I'm somewhat of a PHP newby. I thought I tried that and it wouldn't work. Could you give me a sample snippet? Thanks

tobyontour’s picture

valante> Absolute life-saver. Thanks

valante’s picture

@blackclover, if you mean my method:

Notice that some of the $contexts array items are stdObjects, so the syntax is a little complicated.

Accessing an index in an array: $array_name['index_name']; (index name is enclosed in single/double quotes)

Accessing an object's property: $object_name->property_name; (property name is not enclosed in quotes)

For me, the value I wanted to test was something like this:

return !empty($contexts['argument_entity_id:node_1']->data->your_field_name_here);

Hope that helps!

blackclover’s picture

Thanks, I appreciate the tip. I'll give it t a try.

blackclover’s picture

Thanks, I appreciate the tip. I'll give it t a try.

johnw3600’s picture

Ditto ...thanks.
This has completely opened up what I can do with content filtering!
But given that you can choose such items as 'Context', 'Node:classifications', 'URL', 'User:classifications' etc, its very surprising that the most obvious object, a 'content filter' is not there (or am I missing the point somewhere?)

Nonetheless a combination of comments from Valante:
print_r($contexts... ...and
return !empty($contexts['argument_entity_id:node_1']->data->your_field_name_here);

...allowed me to test against a 'Live' field whether content should be revealed or not:
return $contexts['argument_entity_id:node_1']->data->field_live['und'][0]['value'];
(note that I have multi-lingual features enabled, hence 'und' in this case)

Cheers,
John

bryancasler’s picture

#3 doesn't work for me. I've got php enabled and it's certainly being used.

http://www.diigo.com/item/image/1ks4o/7mts?size=o

There's three scenarios I'd love help figuring out code snippets for.

  1. A text field
  2. A check box (boolean)
  3. A select list (dropdown)

For the snippets lets assume our field name is field_XXXX

Any help would be greatly appreciated.

valante’s picture

@animelion, your PHP is definitely not being used, seeing as the drupal_set_message line is printed to screen instead of being evaluated. Where exactly did you enter this snippet? And did you by any chance surround it with php tags (which shouldn't be there)?

bryancasler’s picture

@valante I think you're right about the php tags, but I can't test that. Running into this problem now #1355300: Unable to Add Content to Panels.

bryancasler’s picture

@valante It worked perfect. Thanks. Going to head over here #1300476: Add Access Plugin for Entity Field Value and here #1356494: Visibility rule becomes broken now.

Ashlar’s picture

Status: Active » Closed (fixed)
a.knutson’s picture

Jeez, that was insanely helpful for me. Thanks a lot Valante.