0

I have a view that has a boolean field in it and a field that links to a file. I want to use the boolean field to where if it is checked, it will direct to a static link, regardless of what file is uploaded. If it is unchecked and there is no file, I want it to output nothing. If it is unchecked and there is a file, then output the file link. I tried setting up Views PHP and setting this up there, no luck. First question is does Views PHP recognize rewrite rules that you've established in other fields, and if not, how can I accomplish this? Below is the code I tried in Views php. All it does is output the node id for every entry.

<?php
  if($row->field_booleanval) {
    echo '<a href="http://www.example.com">Link</a>';
  }
  else if(!($row->field_booleanval) && !($row->field_file)) {
    echo ' ';
  }
  else {
     echo $row->field_file;
  }
?>
  • try if(isset($row->field_booleanval)){ assuming the variable name is correct – Geoff Apr 30 '15 at 16:13
  • Causes them to all return the external link. It's almost like neither of the two fields are returning what I would expect. – Big EMPin Apr 30 '15 at 16:38
  • hmm.. some modules don't pass along values if they don't register true - I guess that one does regardless. how about including the boolean value in the IF statement (if $row->field_booleanval == 1) assuming you used 0 and 1 for the field values – Geoff Apr 30 '15 at 16:51
  • Had already tried that...causes the same behavior as the first case. Not liking this Views PHP module so far. – Big EMPin Apr 30 '15 at 16:59
  • 1
    in the field configuration for booleanval, is it set to default or key? – Geoff Apr 30 '15 at 17:40
1

I finally got it to work as follows:

PHP field, Value Code:

return $data->field_field_booleanval[0]['raw']['value'];

Output Code:

<?php
  print_r($value);
?>

This returns the correct VALUE for the boolean field. So you can skip the Value Code unless you plan on using the variable elsewhere, and output could look something like:

<?php
  $booleanval = $data->field_field_booleanval[0]['raw']['value'];
  $fileval = isset($data->field_field_file[0]); // shouldn't be set if no value

  if($booleanval) {
    echo '<a href="http://www.example.com">Link</a>';
  }
  else if(!$booleanval && !$fileval) {
    echo ' ';
  }
  else {
     echo $row->field_file;
  }
?>
  • Based on Google search results I was heading in a similar direction...tried yours exactly and still got this error when attempting this method: Notice: Undefined property: stdClass::$field_booleanval in __lambda_func() (line 2 of ...\drupal\sites\all\modules\views_php\plugins\views\views_php_handler_field.inc(202) : runtime-created function. Happens for each record in the view. – Big EMPin Apr 30 '15 at 19:11
  • had some errors in structure - Devel straightened it out, changed the code – Geoff Apr 30 '15 at 19:27
  • I'm not sure why, but I still had to add a conditional statement for the boolean value to check whether or not it was set. That did it though. 1,000 thank you's and upvotes to you, sir. – Big EMPin Apr 30 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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