Skip to main content
added 629 characters in body
Source Link
hene
  • 468
  • 5
  • 21

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';
    }
  }
}

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){

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';
    }
  }
}
Source Link
hene
  • 468
  • 5
  • 21

Add class to views row if image fields has content

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){