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.