Testing the values of Views fields I have a View that displays four types of field (field_ba_before, field_photo_gallery, field_body_image, field_image_position).

In my views-view-fields.tpl.php file, I have created a variable called $class. I want the variable of $class to change according to the values of the four fields. 

Not every Views row result will use all of the fields, so in my code, I have used IF statements to check if the field exists.

This is my code:

&lt;?php if(($fields['field_ba_before']-&gt; content || $fields['field_photo_gallery']-&gt; content)): ?&gt;
&lt;?php $class = "content-aside-layered"; ?&gt;
&lt;?php else: ?&gt;
&lt;?php if (($fields['field_body_image']-&gt; content)): ?&gt;

    &lt;?php if (($fields['field_image_position']-&gt; content) == 'image_position_right'): ?&gt;
    &lt;?php $class = "content-aside"; ?&gt;
    &lt;?php endif; ?&gt;

    &lt;?php if (($fields['field_image_position']-&gt; content) == 'image_position_left'): ?&gt;
    &lt;?php $class = "aside-content"; ?&gt;
    &lt;?php endif; ?&gt;

    &lt;?php if (($fields['field_image_position']-&gt; content) == 'image_position_bottom'): ?&gt;
    &lt;?php $class = "content-aside-layered"; ?&gt;
    &lt;?php endif; ?&gt;

    &lt;?php if (($fields['field_image_position']-&gt; content) == 'image_position_default'): ?&gt;
    &lt;?php $class = theme_get_setting('body_image_position'); ?&gt;
    &lt;?php endif; ?&gt;

&lt;?php endif; ?&gt;
&lt;?php endif; ?&gt;


&lt;div class="&lt;?php print $class; ?&gt;"&gt; &lt;/div&gt;


I am not sure why it is not working. Is there a special way of testing the values of Views fields? Thanks for any help that can be offered!
Views provide to the template the html output of the field and not the actual value or a means to detect if it is empty or not. You will thus need to compare the field->content value with the expected html output. Assuming that you use Drupal 7 and you haven't overriden the field output or you haven't set the field as hidden ("exclude from display" option), the default field output is:

opendiv class="field-content"> ... html output here ... closediv>

Steps to achieve what you're trying to:

1) Add a function like the following: (replace "opendiv" and "closediv" with html open and close div tags, I couldn't get the editor not to filter them)


function mymodule_views_field_compare_value($field, $expected_value) {
      if ($field->content == 'opendiv class="field-content">'.$extected_value.'closediv>') {
          return true;
      }
      return false;
  }


2) To check if a field is not empty do:


if (!mymodule_views_field_compare_value($fields['field_ba_before'], '')) {
}


3) To check the value of the image positioning:


if (mymodule_views_field_compare_value($fields['field_image_position'], 'image_position_default')) {
}


This should help. You can use dpm($fields['field_image_position']) (if you're using the devel module) or var_dump($fields['field_image_position']) to check the actual output of the field, in case it is different than just 'image_position_default', I don't know what type of field it is.