1

I have a date field for an item in which I am entering a range of years. I have created a view which displays the start date and end date as individual fields. Typical output looks like this:

Title: Pop Art
Start Date: 1958
End Date: 1970

However, there are several items which do not have an end date because they are ongoing. Even though for the end date field's "No Results Behavior" I have "No results text" set to the word present it renders the start date in place of my rewrite text:

Title: Contemporary Art
Start Date: 1970
End Date: 1970

I want it to do output like this:

Title: Contemporary Art
Start Date: 1970
End Date: present

How can I get the end date to render as the word "present" when no end date is specified?

Note: in my final view I will output the date range as, "1970-present." Above, I formatted my examples as "label: value" for clarity.

2
  • are you using date module?
    – Shabir A.
    Commented Dec 18, 2015 at 16:17
  • Yes, I'm using Date module (Drupal 7).
    – Tantalus
    Commented Dec 18, 2015 at 16:19

2 Answers 2

4

When there is no end date specified for a node it takes both the dates same. The module consider it a one day event.

However, in your case you are making it ongoing by not mentioning the end_date so no results behavior is not going to work. I would suggest to use some global php like this

<?php
if($row->field_date == $row->field_date_1) {
   echo "Present";
} else {
  echo $row->field_date_1;
}
?>

Make sure to include this php field after both start and end date fields, and make the end date fields hidden.

3
  • This got me on the right track. However $row->field_date returns nothing in Views PHP. I've come across this before and the solution was to use the $data variable. I will post my code as a separate answer since I can't figure out how to format a block of code in a comment.
    – Tantalus
    Commented Dec 18, 2015 at 17:01
  • I tried the code on my site and it was working, not sure what you messing at your end
    – Shabir A.
    Commented Dec 18, 2015 at 17:04
  • Is it working now?
    – Shabir A.
    Commented Dec 18, 2015 at 17:07
2

Shabir A.'s answer got me on the right track. However $row->field_date returns nothing for me in Views PHP. I've come across this before and the solution was to use the $data variable. Here's the code I ended up using (my date field is 'field_art_year' the extra field_ is not a typo):

<?php
$startdate = date("Y", strtotime($data->field_field_art_year[0]['raw']['value']));
$enddate = date("Y", strtotime($data->field_field_art_year[0]['raw']['value2']));

if($startdate == $enddate) {
  print $startdate . "&ndash;present";
} else {
  print $startdate . "&ndash;" . $enddate;
}
?>

Addendum: Should wrap this code in if (isset($data->field_field_art_year[0]) { } or else you get errors in the log.

1
  • Addendum: Should wrap this code in if (isset($data->field_field_art_year[0]) { } or else you get errors in the log.
    – Tantalus
    Commented Dec 18, 2015 at 19:17

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.