Adding Class to views row if View style is Unformatted list / Content 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(&amp;$vars) {
foreach($vars['view']-&gt;result as $rkey =&gt; $rval)
{
    $node = node_load($rval-&gt;nid);
    if(isset($node-&gt;field_breite) &amp;&amp; !(empty($node-&gt;field_breite)))
    {
        $vars['classes_array'][$rkey] .= $vars['classes_array'][$rkey] . " " . $node-&gt;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:
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(&amp;$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.