1

I have tried to configure a view for a user listing. In this view only few fields from the user entity will be shown. In the user entity I have a boolean variable. If the boolean variable is TRUE ("1") the real profile image should be shown, otherwise the default image for all users should be shown.

In order to implement it I have added a views field of type Global: PHP. In the field's code I wrote:

$file = file_load($row->picture);
if(isset($row->field_primum) ) {
if($row->field_primum == "1") {
print theme('image', array('path' => $file->uri)); 
else ($row->field_primum == "2") {
print(<a  src="/sites/all/themes/test_them/images/anonymous.jpg" height="42" width="42"></a>);
}
}
}

However it runs only on the first user and not for all users in the display. Do you have any idea why?

Thanks zanet

1 Answer 1

0

It's possibly the result of syntax errors.

This is where you can see the benefits of well formatted code.

If you format your code like this it becomes more obvious that you have errors in your code (I assume $row->picture actually is a file ID):

$file = file_load($row->picture);
if (isset($row->field_primum)) {
  if ($row->field_primum == "1") {
    print theme('image', array('path' => $file->uri)); 
    else ($row->field_primum == "2") {
      print(<a src="/sites/all/themes/test_them/images/anonymous.jpg" height="42" width="42"></a>);
    }
  }
}

Problems I can see now are:

  1. There you can see that the else actually has no if statement because it is inside the second if statement.
  2. The else also shouldn't have the condition. If it needs the condition then use elseif.
  3. You mention that the field you are using is a boolean field, in which case the values will be 0 & 1, not 1 & 2.
  4. The second print statement should be printing a string so the string should be wrapped in quotes.
  5. You are incorrectly using the a tag to print an image. You should use an img tag for images and an a tag for links.

I would think it should be more like this:

if (isset($row->field_primum)) {
  if ($row->field_primum) {
    $file = file_load($row->picture);
    print theme('image', array('path' => $file->uri));
  }
  else { 
    print('<img src="/sites/all/themes/test_them/images/anonymous.jpg" height="42" width="42" />');
  }
}

Note that I also moved the file_load() call inside the if statements because there is no need to load it if you aren't using it.

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.