4

I have a view with a page display showing a list of events. I'd like to add a class to the rows of those events that are in the past.

The problem I have is that views-view-list.tpl.php doesn't have access to the values of fields within the rows.

And I can use views-view-fields.tpl.php to check the date field of one row, and wrap each field in that row with a div or span based on whether or not the date field is in the past.

I just can't figure out how to use the value of a field within a row to wrap the whole row in something with a class.

1 Answer 1

7

The following will add the class 'hello' to the li tag. I used a view with machine name 'dates' and a display name of 'page'. The time field is the node created time. You can just install Devel and uncomment the dpm($vars) statement to get what you need. I also left the conditional logic up to you.

Put this in your theme's template.php file:

function MYTHEME_preprocess_views_view_list(&$vars) {
  if ($vars['view']->name == 'dates' && $vars['view']->current_display == "page") {
    // dpm($vars); //uncomment to see variables
    foreach($vars['view']->result as $key => $value){
      $timestamp = $value->node_created;
      if ($timestamp) { // add whatever conditional logic you need here
        $vars['classes_array'][$key] .= ' hello'; // note the space in front of class
      }
    }
  }
}

An alternative method is as follows:

Put this in your theme's template.php file:

function MYTHEME_preprocess_views_view_fields(&$vars) {
  if ($vars['view']->name == 'dates' && $vars['view']->current_display == "page") {
    $timestamp = $vars['row']->node_created;
    if ($timestamp < strtotime('now')) {
      $vars['classes_array'][] = 'bye';
    }
  }
}

and use this views-view-fields--VIEWNAME.tpl.php file:

<div class="<?php print $classes;?>">
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
  <?php print $field->label_html; ?>
  <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
</div>
4
  • 1
    OMG OMG OMG THIS TOTALLY WORKED (I used the first chunk of code in my template.php). Thank you SO. MUCH.
    – user5035
    Commented Aug 27, 2014 at 19:33
  • Is there a way to do this by only using the template file?
    – stfn
    Commented Jan 26, 2015 at 0:37
  • You will have to move the logic that is in the preprocess function into the beginning of the template, and add the class to the $classes variable (not the classes_array variable). The variables that in the preprocess function is accessed under $vars will have been flattened so that in the template file you can access them directly, ie $vars['view'] will become $view. Commented Jan 26, 2015 at 4:02
  • couldn't user $vars['classes_array'][$key] for table style because of Notice: Undefined offset: 2 error where 2 is the found key. Is this only works for non-table styles
    – kiranking
    Commented Dec 3, 2015 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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