0

I have views which has multiple image fields. In some cases all those image fields are empty, but some cases one of them has content.

When image field has content I want to have extra class .has-image for views row.

There is image fields: image_field_1, image_field_2 and image_field_3

It can be done programmatically, but I'm lost how to check those fields. I know this much:

function YOURTHEME_preprocess_views_view_unformatted(&$vars){


SOLUTION

There can not be used $vars['classes_array'][$id][] because $classes_array[$id] is not an array.

Here is code for working solution.

function theme_preprocess_views_view_unformatted(&$vars) {
  $view = $vars['view'];
  $rows = $vars['rows'];

foreach ($vars['view']->result as $id => $row) {

    if (!empty($row->field_field_image) || !empty($row->field_field_image_2) || !empty($row->field_field_image_3)) {
      $vars['classes_array'][$id] .= ' has_field';
    }  
    else {
      $vars['classes_array'][$id] .= ' has_no_field';
    }
  }
}
3
  • assuming the class only affects the images, why not just include the row class under settings, regardless of whether or not there is an image present?
    – Geoff
    Commented Jul 28, 2014 at 14:27
  • Nope. I'm trying to make this and seems that it is not possible without creating extra class for those views rows which has image: stackoverflow.com/questions/24991350/…
    – hene
    Commented Jul 28, 2014 at 14:30
  • see if this helps (drupal.stackexchange.com/questions/3059/…)
    – Geoff
    Commented Jul 28, 2014 at 14:36

2 Answers 2

2

When you use the preprocess function you find the result in

$vars['view']->result

and the corresponding classes array under

$vars['classes_array']

where in results and in classes array the corresponding rows have the same key.

You can do now something like this in your preprocess function:

foreach ($vars['view']->result as $rid => $row) {
  if (isset($row->YOUR_FIELD) && !empty($row->YOUR_FIELD)) {
    $vars['classes_array'][$rid][] = 'has_field';
  }  
  else {
    $vars['classes_array'][$rid][] = 'has_no_field';
  }
}

It is untested and just a little scribble but maybe it helps you.

5
  • I'm getting: Fatal error: [] operator not supported for strings in. Any help?
    – hene
    Commented Jul 31, 2014 at 9:40
  • I got it work by using this, but now it adds same class (has_field) to every row even if field_image has content or does not have : function theme_preprocess_views_view_unformatted(&$vars) { $view = $vars['view']; $rows = $vars['rows']; foreach ($vars['view']->result as $id => $row) { if (isset($row->field_image)) { $vars['classes_array'][$id] = 'has_field'; } else { $vars['classes_array'][$id] = 'has_no_field'; } } }
    – hene
    Commented Jul 31, 2014 at 11:10
  • Hmm, could it be because I use one field to show all content. So I have field which has Rewrite results and then tokens. So it basically loads all fields no matter if they does not have content? Here is my field Rewrite results: <div class="front_tile_overlay"></div><div class="tile_image">[field_image][field_image_2][field_image_3]</div><div class="tile_content"><div class="front_tile_title">[field_tile_title]</div><div class="front_tile_body">[field_text_to_frontpage_box]</div></div>
    – hene
    Commented Jul 31, 2014 at 11:27
  • So code should check if field has value (content) or not. Any tips?
    – hene
    Commented Jul 31, 2014 at 12:11
  • Do you use devel module? There is a function dpm() which shows you the content of an array or anything else. Maybe add dpm($vars['view']->result); to your preprocess function to find out how the field looks like when there is content and when there is no content. Commented Jul 31, 2014 at 14:26
0

An alternative solution to using a preprocess function (because I wanted to override this in a module not the theme and didn't want to delve into the possibility of overriding the theme registry), I did the following:

  1. created a custom version of the view's theme template Style Output: views-view-unformatted.tpl.php, by removing the div wrapper within the foreach loop
  2. created a custom version of Row style output: views-view-fields.tpl.php by wrapping the fields with a div there and adding class names based on the field data in $row->field_YOUR_FIELDNAME[0]['raw']['value']

I had a boolean field I was checking, so you may want to log (with watchdog) or print out $row->field_YOUR_FIELDNAME[0] and see what's available.

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.