Using Views Custom Field

On a recent project I found the need to print an image in a view. The image would be coming from one of three possible places.

1) An ImageField image upload.
2) The value of a text field.
3) A default location on the server.

I only wanted 1 image to show up in the view. I used the http://drupal.org/project/views_customfield module to add the image based on conditional logic.

Basically, if an image was uploaded via the ImageField CCK widget, I wanted that image to appear, if there had not been an image uploaded, but there was a value in the textfield, I wanted that image to be displayed. Otherwise, if both of those fields were empty, then I wanted to print a default image from the server.

Below is the logic I put into the Views Customfield PHP field.

<?php
 
if ($data->node_data_field_location_field_apt_image_fid) {
   
$img_apt_img = field_file_load($data->node_data_field_location_field_apt_image_fid);
   
$php_arg = theme('imagecache', '200wide', $img_apt_img['filepath']);
  }
  elseif (
$data->node_data_field_location_field_defaultimage_value) {
   
$php_arg = "<img src=\"" . $data->node_data_field_location_field_defaultimage_value . "\" width=200px";
  }
  else {
   
$php_arg = "<img src=\"http://example.com/themes/mytheme/images/no-photo.jpg\"";
  }
  print
$php_arg;
?>

I selected the "Exclude from display" checkbox so that the value would not be printed to the screen. The reason for this is because I was using the Views Custom Field of type "Markup" to create my view.

So now inside of my Markup field, I added <div>[phpcode]</div> where I wanted the image to appear.

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.