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:

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

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

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

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

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

<?php endif; ?>
<?php endif; ?>

<div class="<?php print $class; ?>"> </div>

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!

  • Dont use too common views template: views-view-fields.tpl.php - instead of this click on Information in Views editing, there's try select last style (rows, or field), and access fields values from $rows variable. For investigating this structure, use Devel module and use next code: dsm();
    – Nikit
    Jun 2, 2013 at 4:28

1 Answer 1


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.


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.