3

Panels allows selection rules to be created with custom PHP code (disclaimer: I know this approach is insecure and should be avoided).

When adding PHP code as a selection rule, the helpful description states:

Access will be granted if the following PHP code returns TRUE. Do not include . Note that executing incorrect PHP-code can break your Drupal site. All contexts will be available in the $contexts variable.

I made a function in a custom module to use for the selection rule:

function mymodule_selection_rule_is_location_set() {
  global $user;
  $user_fields = user_load($user->uid);
  if (isset($user_fields->field_acc_location['und']['0']['lat'])) {
    $location = $user_fields->field_acc_location['und']['0']['lat'];
  }
  if (empty($location)) {
    return FALSE;
  }
  else {
    return TRUE;
  }
}

However, when I set the selection rule as shown below, the selection rule is not triggered even when there is a value for $location and the function returns TRUE (confirmed with devel module).

Evaluates false as a selection rule even when the function returns TRUE

mymodule_selection_rule_is_location_set()

To get it to work, I had to write the selection rule like this:

Evaluates correctly

if (mymodule_selection_rule_is_location_set()) {
  return TRUE;
}

My question is why do I have to write it like the latter rather than the former?

2
  • 1
    The first one is missing a ;, could that be the problem?
    – penten
    Commented Feb 5, 2013 at 9:18
  • Nope, it evaluates incorrectly whether there is a ; or not. Commented Feb 5, 2013 at 9:28

1 Answer 1

2

I think the calling function which executes your PHP code would expect the RETURN true/false statement. Thus, you would need return to the calling function as returned by the called function.

return mymodule_selection_rule_is_location_set();

If you don't have return, the calling function would expect NULL and it would execute as FALSE always regardless of what is returned by the called function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.