views:

382

answers:

1

I'm trying to dream up a way to institute color coding in Drupal views. I have several needs for this but can't figure out how to do it. Here are some examples...

  1. In a table view for a content type, I want to color code each row based on the age of the post. In other words, "age" of the post is one of the columns in the table, and I want each row whose post is less than a day old to be highlighted with a yellow background. If it's over a week, highlight with red, and so on...

Anyone have ideas for this? I suspect we could probably grab the conditional values in a normal web page, but this is tricky in Drupal and my javascript knowledge is limited. I know with good ole SQL we could run some PHP on the values and associate a css selector to do this, but I'm trying to accomplish it in views (the contributed modlue). Thanks in advance

A: 

I would create a views-view-table--Temp.tpl.php (where Temp is the view name) that inserts in the classes depending on the date. Here is an example where I changed the created date to show Time Ago and just searched on Week or Day with stripos. You can use php date math or other methods to insert your class. This is very basic and will need tweaking:

<?php
// $Id: views-view-table.tpl.php,v 1.8 2009/01/28 00:43:43 merlinofchaos Exp $
/**
 * @file views-view-table.tpl.php
 * Template to display a view as a table.
 *
 * - $title : The title of this group of rows.  May be empty.
 * - $header: An array of header labels keyed by field id.
 * - $fields: An array of CSS IDs to use for each field id.
 * - $class: A class or classes to apply to the table, based on settings.
 * - $row_classes: An array of classes to apply to each row, indexed by row
 *   number. This matches the index in $rows.
 * - $rows: An array of row items. Each row is an array of content.
 *   $rows are keyed by row number, fields within rows are keyed by field ID.
 * @ingroup views_templates
 */
?>
<table class="<?php print $class; ?>">
  <?php if (!empty($title)) : ?>
    <caption><?php print $title; ?></caption>
  <?php endif; ?>
  <thead>
    <tr>
      <?php foreach ($header as $field => $label): ?>
        <th class="views-field views-field-<?php print $fields[$field]; ?>">
          <?php print $label; ?>
        </th>
      <?php endforeach; ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print implode(' ', $row_classes[$count]); ?> <?php
        if(stripos($row["created"], "Week")) {
                print "week-class ";
        }
        if(stripos($row["created"], "Day")) {
                print "day-class ";
        }?>
        ">
        <?php foreach ($row as $field => $content): ?>
          <td class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $content; ?>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
Jeremy Heslop
I haven't tested this yet, but your logic is right on. Thanks
cinqoTimo