You are here

public function IcalFieldsWizard::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:

1 call to IcalFieldsWizard::addDateRangeEvent()
IcalFieldsWizard::render in src/Plugin/views/row/IcalFieldsWizard.php
Render a row object. This usually passes through to a theme template of some form, but not always.

File

src/Plugin/views/row/IcalFieldsWizard.php, line 345
Contains \Drupal\views_ical\Plugin\views\row\Fields.

Class

IcalFieldsWizard
The 'Ical Fields' row plugin

Namespace

Drupal\views_ical\Plugin\views\row

Code

public function addDateRangeEvent(array &$events, ResultRow $row, \DateTimeZone $timezone, array $field_mapping) : void {
  $entity = $this
    ->getEntity($row);
  $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, $row);

    // 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;
  }
}