You are here

public function ViewsIcalHelper::addDateRangeEvent in Views iCal 8

Create an event based on a daterange field.

Parameters

array $events:

\Drupal\Core\Entity\ContentEntityInterface $entity:

\DateTimeZone $timezone:

array $field_mapping:

File

src/ViewsIcalHelper.php, line 170

Class

ViewsIcalHelper
Helper methods for views_ical.

Namespace

Drupal\views_ical

Code

public function addDateRangeEvent(array &$events, ResultRow $row, \DateTimeZone $timezone, array $field_mapping) : void {
  $entity = $row->_entity;
  $utc_timezone = new \DateTimeZone('UTC');
  $datefield_values = $entity
    ->get($field_mapping['date_field'])
    ->getValue();

  // TODO: make these separate functions
  // Loop over the values to support multiple cardinality dates, which can
  // represent multiple events.
  foreach ($entity
    ->get($field_mapping['date_field'])
    ->getValue() as $date_entry) {

    // generate the event.
    $event = $this
      ->createDefaultEvent($entity, $field_mapping);

    // Set the start time
    $start_datetime = new \DateTime($date_entry['value'], $utc_timezone);
    $start_datetime
      ->setTimezone($timezone);
    $event
      ->setDtStart($start_datetime);

    // Loop over field values so we can support daterange fields with multiple cardinality.
    if (!empty($date_entry['end_value'])) {
      $end_datetime = new \DateTime($date_entry['end_value'], $utc_timezone);
      $end_datetime
        ->setTimezone($timezone);
      $event
        ->setDtEnd($end_datetime);

      // If this is a date_all_day field, pull the all day option from that.
      if ($date_all_day = false) {

        // TODO: implement
      }
      else {
        if (isset($field_mapping['no_time_field']) && $field_mapping['no_time_field'] != 'none') {
          $all_day = $entity
            ->get($field_mapping['no_time_field'])
            ->getValue();
          if ($all_day && isset($all_day[0]['value']) && $all_day[0]['value']) {
            $event
              ->setNoTime(true);
          }
        }
      }
    }

    //else {

    // is DTEND is not a required field, but if it is not included, nor
    // is duration (which we are not using here), then the event's duration
    // is taken to be one day. But do we need to explicitly define that here?
    // Do calendar apps handle that? https://tools.ietf.org/html/rfc5545#section-3.6.1

    //}
    $events[] = $event;
  }
}