You are here

public function ViewsIcalHelper::addDateTimeEvent in Views iCal 8

Create an event based on a datetime field

Parameters

array $events:

\Drupal\Core\Entity\ContentEntityInterface $entity:

\DateTimeZone $timezone:

array $field_mapping:

File

src/ViewsIcalHelper.php, line 230

Class

ViewsIcalHelper
Helper methods for views_ical.

Namespace

Drupal\views_ical

Code

public function addDateTimeEvent(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();

  // If an end date field was defined, then the content model is most likely
  // using two, single cardinality fields for a start and an end date.
  if (isset($field_mapping['end_date_field']) && $field_mapping['end_date_field'] != 'none') {

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

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

    // Set the end time
    $end_date_field_values = $entity
      ->get($field_mapping['end_date_field'])
      ->getValue();
    $end_date_entry = $end_date_field_values[0];
    $end_datetime = new \DateTime($end_date_entry['value'], $utc_timezone);
    $end_datetime
      ->setTimezone($timezone);
    $event
      ->setDtEnd($end_datetime);

    // All day events.
    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);
      }
    }
    $events[] = $event;
  }
}