1

I am working on an art gallery site in which we have two views that are filtered based on the date range of an exhibition. One view filters current exhibits (now >= start date && now <= end date) and another that filters upcoming exhibitions (now < start date).

We would like to combine these into a single page view. This is simple enough to do with this date filter: now <= end date. However, I would still like to denote which events are upcoming (be it a CSS class or by rendering the date field as "Opens on Dec. 12th").

Is there a way to render/rewrite the date field differently based on the field's value relative to today's date?

For example, if today's date is Nov. 1, 2015, I would like these two nodes:

  • Exhibit #1 (Oct. 1, 2015 - Dec. 1, 2015)
  • Exhibit #2 (Jan. 1, 2016 - Feb. 1, 2016)

To render like this:

  • Exhibit #1, Ends Dec. 1 2015
  • Exhibit #2, Opens Jan 1, 2016
2
  • I assume you are able to modify your theme? Also, are you using the "Unformatted list" style? Commented Oct 8, 2015 at 12:35
  • Actually, this will be outputting as a Masonry style dynamic layout (like Pinterest and google+ use). The issue here is more in getting the rewrite results to look at the date value and change the output based on that value.
    – Tantalus
    Commented Oct 8, 2015 at 21:37

2 Answers 2

1

A possible solution is to use the Views feature "rewrite result" (as explained also in Rewriting view fields), combined with the Views PHP module.

If you're not experienced in PHP, you may want to experiment with only the "rewrite result" feature, which may help to come up with an acceptable compromise for what you're looking for.;

Note: The Views PHP module only has an alfa version as of today, but it's used in about 70K sites anyway ... That many sites can't be wrong, right?

1
  • I will look into this. Although I am not an experienced PHP programmer. Thanks.
    – Tantalus
    Commented Oct 8, 2015 at 21:39
0

Following Pierre's suggestion, I installed the "Views PHP" module. Here's how I set up my view:

  1. Added my date field (field_dates) and then Global: PHP field
  2. Make sure date field is "Excluded from Display"
  3. Edit the "Output code" of the Global: PHP field to the following

PHP Code:

<?php
$start_date = strtotime($data->field_field_dates[0]['raw']['value']);
$end_date   = strtotime($data->field_field_dates[0]['raw']['value2']);
if  (time() < $start_date) {
   echo 'Starts on ';
   echo date("F j, Y", $start_date);
} else if (time() < $end_date) {
   echo 'Open Until ';
   echo date("F j, Y", $end_date);
} else  {
    echo 'Closed on ';
    echo date("F j, Y", $end_date);
}
?>

Note, field_field_dates is not a typo. That extra field_ is required for the code to work. Using the values listed in my questions example, the results are:

  • Exhibit #1, Open Until December 1, 2015
  • Exhibit #2, Starts on January 1, 2016

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.