This question already has an answer here:

I need to add a CSS class to a views row depending on a field. So far this is easy going if I use Fields as Content (add class via replacement patterns then).

But I find it far more difficult to do this when using Row style "Content" and a custom view mode (e. g. Teaser).

I found the following code to work for me if I use Row style "Content":

function mytheme_preprocess_views_view_unformatted(&$vars) {
foreach($vars['view']->result as $rkey => $rval)
    $node = node_load($rval->nid);
    if(isset($node->field_breite) && !(empty($node->field_breite)))
        $vars['classes_array'][$rkey] .= $vars['classes_array'][$rkey] . " " . $node->field_breite[LANGUAGE_NONE][0]['value'];


I like to ask if this is a suitable approach or am I maybe wasting resources by loading each node and can I grab the node data easier somewhere else?

PS: It is not a option for me here to use Fields in the View as content because I want to use different content types in the view with different fields.

Settings: enter image description here

marked as duplicate by Yusef Mohamadi, Scott Joudry, Shawn Conn, Mołot, Krishna Mohan Nov 18 '15 at 4:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Can you Tell my what you want to do, you just want add class to your views row? – Yusef Mohamadi Nov 16 '15 at 21:09
  • Yes, just want to add a class to the view, but based on a field value in the related node. – Stefan Korn Nov 16 '15 at 23:00
  • I don't understand, you want add a class of fields in row equal to specified value? and you want only use unformatted in Row Style Format? – Yusef Mohamadi Nov 17 '15 at 5:20
  • Yes, I want to add the value of a field as a class to the views row and I am using the Views Row Style "Content". Including a screenshot of the settings. – Stefan Korn Nov 17 '15 at 8:46
  • what is the problem of you code? It seems work correctly – Yusef Mohamadi Nov 17 '15 at 18:13

You can try do this later in process or template, when the $vars['rows'] is flattened. You can pull all instances of field_breite out of the flattened row and put them into the classes of the row. For this the field_breite has to be in the custom view mode of course. This would be not so nice for coding, but much faster.

I would code it like this in mytheme_process_views_view(&$vars), but I'm sure you need some debugging:

Go row by row in $vars['rows']. Search the complete field with preg_match() by using the html tags of the field and set parenthesis in the regular expression to get the field value in $matches[1]. Add the field value in $matches[1] to $vars['classes_array'][$id] to set the row class. Cut the complete field out of the row with str_replace() by using $matches[0] from the first search and an empty string.

  • You mean by search and replace the html output and I would need the field in the view mode and hide it by CSS? – Stefan Korn Nov 17 '15 at 16:35
  • No, my idea was to remove the field from the row, after using it to extract the row class. See my edited answer. – 4k4 Nov 17 '15 at 17:47
  • as far as I can see $vars['rows'] is one big HTML string. While the search and replace might be feasible it will not work to put the class to var['classes'] because these are the classes for the view and not for the row. – Stefan Korn Nov 18 '15 at 7:52
  • You are right, the row classes are in $classes_array[$id]. The coding is not the easiest, but the fastest way that came into my mind. Otherwise your original code is fine, but 10 or 100 times slower. – 4k4 Nov 18 '15 at 8:57
  • Ok, but it seems $classes_array[$id] also contains the view classes and not the row classes. Thanks for your input, seems that there is no very easy solution to that, so maybe I leave it with my code since I am not having that much content in this view. – Stefan Korn Nov 18 '15 at 10:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.