Using Drupal 7 and Views, I currently have a view that produces a list of 5 teasers of the content type "News." 5 teasers is a hard rule. I'd like to conditionally format the rows so that when their "created date" is less than 14 days old so that I may styles them as "New" with CSS styles. So if there are 3 "New" rows, the balance are 2 unmarked rows.
How to do so?
Update
I had the idea of using the "time ago" date format to populate the class name. However, the format for "time ago" changes. Unfortunately, I can't set the granularity to "days" which would have allowed me to add the text as a class, i.e., date1 through date24 for the past 2 weeks.
Update #2
My genesis subtheme is ignoring any preprocessor routines, see more here. However, I can add the routine to my base template.php.
Update #3
Thanks to Елин Й. for the working code:
function genesis_preprocess_views_view_list(&$variables) {
$view = $variables['view'];
if ($view->name == 'recent_news_widget') {
foreach ($view->result as $key => $result) {
if (time() - strtotime($result->node_created_day) < 14 * 24 * 60 * 60) {
$variables['classes'][$key][] = 'new';
$variables['classes_array'][$key] = isset($variables['classes'][$key]) ? implode(' ', $variables['classes'][$key]) : '';
}
}
}
}
In an offline discussion, Елин Й. explains why this works; "I took a look in the views theme.inc and found out that, the classes array is flattened to another variable called 'classes_array' you need to do it as well in order to get your new class also added in this array.
row_class
completely. You want to append your own class -- not nuke everything to new.row_class
array instead of overwriting it.