Drupal - D7: Add a class to a list item when the created date is less than 14 days old
You can do that using a preprocessor function for the view you created. You need to preprocess the view and check the creation date of nodes for each row and give them a relating class.
Given that your view's machine name is recent_news_widget
, your diplay format is HTML lists, and your theme name is genesis
. You need to use template_preprocess_views_view_list() function in /sites/all/themes/genesis/template.php file
genesis_preprocess_views_view_list(&$variables) {
$view = $variables['view'];
if ($view->name == 'recent_news_widget') {
// dpm($view);
// Here comes your logic
}
}
In the above code you see 2 lines commented out. First one is a helpful function for developers to print out the given array/object/string etc in a browsable style. This function comes with Devel module, which I would strongly recommend to install it.
After browsing the variables, you will probably find where the results are listed and where the classes for the rows are assigned. You need to browse the variables until you find all values you need to build your logic to assign new classes, for example the 'created date' of the node. After you have all the variables you need, you can loop through the results, check the creation date and add a relating class.
UPDATE: The preprocess functions for other display types than HTML List:
Grid: template_preprocess_views_view_grid() Table: template_preprocess_views_view_table() Unformatted: template_preprocess_views_view_unformatted()
UPDATE #2 (According the discussion on chat and your updates on the question): I find the code in your Update #3 was in the right direction, but with a few mistakes. The corrected version should look like that:
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]) : '';
}
}
}
}
The 2 mistakes in your code were:
You passed the argument as &$variables to the function, but used $vars to assign classes, $vars is not defined in the function so the class was not added.
You wanted to use the key number of the result but in
foreach
you loop only through the values, and$result
variable is an object. In the corrected version of your code above, I used$key => $result
by defining the loop so that I can access the key number as well.
UPDATE #3 I added a new line that flattens the classes into classes_array.
Hope this will help you!!
As you need to add the class to the row, you can achieve by overriding the default view template file
You can achieve this by the following steps
Open your view and go to Advance tab + other category you can see the theme - information click Information link.
As per your requirement you need to override the row style. you can see Row style output, the default template file is views-view-fields.tpl.php which is highlighted. Means this template file is active now. And also you see list of tpls will be there, so choose any one of the tpl name (eg.., views-view-fields--comments-recent.tpl.php or views-view-fields--comments-recent--page.tpl.php) for your new template file.
You need to override this template file so navigate to VIEWS module + THEME folder + get the tpl file which you want to override(here views-view-fields.tpl.php)
Copy and paste template file to your theme template folder(sites/themes/your_theme/templates/) and rename it to the choose template name
Click Rescan template file to active new template file.
- No you can modify the template file and add the class.