0

I have an 'event' node-type with just a body and a date-field. For that date-field, multiple dates are allowed.

I'm listing event-nodes in a view and already use filters to only display nodes when the latest date is after 'now', so that completely outdated events do not get listed automatically. However, when I have multiple dates and at least one of those is still in the future, the node gets displayed.

In the latter case, how can hide all dates that are in the past, and only display the ones that are still in the future? I didn't find an option for that for the date-field in the node-type display settings.

I guess I could override my node.tpl.php with a custom node--event.tpl.php, but how do I parse out single dates of my date-field then, check if they are in the past and hide those that are? I'm totally lost when it comes to PHP and Drupal content-arrays.. I'd appreciate any help with that!

0

Ok, I dived into the drupal variables with the help of the devel module and came up with a solution (first version, see better version below the edit):

I now parsing all dates directly from the content in my specific node--event.tpl.php. I then iterate through them and store all dates that are in the past in a new array. In the end, if that array contains any values and my new option is activated (differentiating between teaser and full display), I remove those past-dates from the date-field array in the content:

<?php
// remove dates in the past from the date-field if enabled
if (isset($node->field_hide_past_dates) && !empty($node->field_hide_past_dates)) {
  $option = $node->field_hide_past_dates['und'][0]['value'];
  if (($option == "teaser" && $teaser) || $option == "always") {
    $UTC = new DateTimeZone("UTC");
    $now_utc_datetime = new DateTime( "now", $UTC );
    $past_date_array = array();
    foreach($content['field_eventdate']['#items'] AS $date_array) {
      $to_date_utc_string = $date_array['value2'];
      $to_date_utc_datetime = date_create_from_format("Y-m-d H:i:s", $to_date_utc_string, $UTC);
      if ($to_date_utc_datetime < $now_utc_datetime) {
        array_push($past_date_array, $date_array);
      }
    }

    if (count($past_date_array) > 0) {
      $only_future_date_array = array_filter($content['field_eventdate']['#items'], function ($var) use ($past_date_array) {
        return !in_array($var, $past_date_array);
      });
      $content['field_eventdate']['#items'] = $only_future_date_array;
    }
  }
}
?>

Maybe there is a cleaner way of preprocessing the content (let me know), but this was the quickest I could come up with and I now have the option per-node.

== EDIT ==

Why make it complicated: I forgot about hiding fields, so no more array-assigning, just on-the-fly hiding of past-dates:

if (isset($node->field_hide_past_dates) && !empty($node->field_hide_past_dates)) {
  $option = $node->field_hide_past_dates['und'][0]['value'];
  if (($option == "teaser" && $teaser) || $option == "always") {
    $UTC = new DateTimeZone("UTC");
    $now_utc_datetime = new DateTime( "now", $UTC );
    foreach($content['field_eventdate']['#items'] AS $key =>  $date_array) {
      $to_date_utc_string = $date_array['value2'];
      $to_date_utc_datetime = date_create_from_format("Y-m-d H:i:s", $to_date_utc_string, $UTC);
      if ($to_date_utc_datetime < $now_utc_datetime) {
        hide($content['field_eventdate'][$key]);
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.