Skip to main content
Tweeted twitter.com/StackDrupal/status/801375371404918785
Question Protected by avpaderno
added 961 characters in body; deleted 961 characters in body
Source Link
JGrubb
  • 377
  • 1
  • 2
  • 12
Source Link
JGrubb
  • 377
  • 1
  • 2
  • 12

Dynamically adding a CSS class to a view row with template.php

I've actually reached the bottom of Google trying to figure out how to add a css class to each row of a view. The trick is that I need the class for each row to be dynamically determined based on some of the data from the node that the view pulls from. The function that pulls this off neatly for the node is -

function pgc_preprocess(&$variables) {
  $node = $variables['node'];
  if ($node->type == "event") {
    $variables['event_class'] = '';
    $num_trainers = $node->field_number_of_trainers[0]['value'];
    $count = count($node->field_trainer);
    if($count < $num_trainers) {
        $variables['event_class'] = 'red';
    } else {
        $variables['event_class'] = 'green';
    }
    return $variables;
  }
}

The point of this is to color code an event that hasn't had enough folks sign up. There will be a list of events on the front page, and I need them to be color coded also. I really hope there some simple solution along the lines of -

function pgc_preprocess_views_view_unformatted(&$variables) {
  // Magic here, preferably having something to 
  // do with the function I already wrote.
}

Just dropping <?php print $event_class ?> in the view .tpl doesn't do it.