I have a view showing taxonomy terms with an images field. I'd like to add a class .has-images if the images field is not empty.

I've done this before with nodes using preprocess, have tried something similar and have looked around for an answer though I can't seem to get it to work. Also where would the code go? would it go in a view.preprocess.inc file?

This is an example of node.preprocess.inc

function THEMENAME_preprocess_node(&$vars) {
 if(!empty($vars['field_images'])) {
    $vars['classes_array'][] = 'has-images';

Any help would be much appreciated.

  • Can be done with Views. On image field use 'REWRITE RESULTS' to add class in wrapper div, and in 'NO RESULTS BEHAVIOR' make sure 'Hide rewriting if empty' is ticked. – J. Reynolds Feb 21 '15 at 19:23
  • @J.Reynolds that would work, though that adds the class to the images field. I need it to add a class to the list item .views-row so that I can effect the width of the text based on if there are images. Currently there is just a large gap. – th0ward Feb 21 '15 at 20:08

You should try hook_views_pre_render(&$view); http://api.drupal.org/api/views/views.api.php/function/hook_views_pre_render/7

Also you can write a template file to write extra css class with condtion. For this go to advance section and go for theming section and choose a template name.

Your Answer

