You are here

protected function FullCalendar::prepareEvents in FullCalendar 8

Same name and namespace in other branches
  1. 8.5 src/Plugin/views/style/FullCalendar.php \Drupal\fullcalendar\Plugin\views\style\FullCalendar::prepareEvents()
  2. 8.2 src/Plugin/views/style/FullCalendar.php \Drupal\fullcalendar\Plugin\views\style\FullCalendar::prepareEvents()
  3. 8.3 src/Plugin/views/style/FullCalendar.php \Drupal\fullcalendar\Plugin\views\style\FullCalendar::prepareEvents()
  4. 8.4 src/Plugin/views/style/FullCalendar.php \Drupal\fullcalendar\Plugin\views\style\FullCalendar::prepareEvents()

Prepare events for calendar.

Return value

array Array of events ready for fullcalendar.

Throws

\Exception

1 call to FullCalendar::prepareEvents()
FullCalendar::prepareSettings in src/Plugin/views/style/FullCalendar.php
Prepare JavaScript settings.

File

src/Plugin/views/style/FullCalendar.php, line 389

Class

FullCalendar
Plugin annotation @ViewsStyle( id = "fullcalendar", title = @Translation("FullCalendar"), help = @Translation("Displays items on a calendar."), theme = "fullcalendar", theme_file = "fullcalendar.theme.inc", display_types = {"normal"} )

Namespace

Drupal\fullcalendar\Plugin\views\style

Code

protected function prepareEvents() {
  $events = [];
  foreach ($this->view->result as $delta => $row) {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $row->_entity;

    // Collect all fields for the customize options.
    $fields = [];

    // Collect only date fields.
    $date_fields = [];

    // Collect prepared events.
    $event = [];

    /* @var \Drupal\views\Plugin\views\field\Field $field */
    foreach ($this->view->field as $field_name => $field) {
      $fields[$field_name] = $this
        ->getField($delta, $field_name);
      if (fullcalendar_field_is_date($field)) {
        $field_storage_definitions = $this->fieldManager
          ->getFieldStorageDefinitions($field->definition['entity_type']);
        $field_definition = $field_storage_definitions[$field->definition['field_name']];
        $date_fields[$field_name] = [
          'value' => $field
            ->getItems($row),
          'field_alias' => $field->field_alias,
          'field_name' => $field_definition
            ->getName(),
          'field_info' => $field_definition,
          'timezone_override' => $field->options['settings']['timezone_override'],
        ];
      }
    }

    // If using a custom date field, filter the fields to process.
    if (!empty($this->options['fields']['date'])) {
      $date_fields = array_intersect_key($date_fields, $this->options['fields']['date_field']);
    }

    // If there are no date fields (gcal only), return.
    if (empty($date_fields)) {
      return $events;
    }
    foreach ($date_fields as $field) {

      // Filter fields without value.
      if (empty($field['value'])) {
        continue;
      }

      /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_definition */
      $field_definition = $field['field_info'];

      // Get 'min' and 'max' dates appear in the Calendar.
      $date_range = $this
        ->getExposedDates($field['field_name']);

      // "date_recur" field (with recurring date).
      if ($field_definition
        ->getType() == 'date_recur') {

        /** @var \Drupal\date_recur\Plugin\Field\FieldType\DateRecurFieldItemList $field_items */
        $field_items = $row->_entity->{$field['field_name']};
        $isRecurring = FALSE;

        /** @var \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem $item */
        foreach ($field_items as $index => $item) {

          // Get DateRecur Occurrence Handler.
          $occurrenceHandler = $item
            ->getOccurrenceHandler();

          // If this field is a DateRecur field.
          if ($occurrenceHandler
            ->isRecurring()) {

            // Get a list of occurrences for display.
            $occurrences = $occurrenceHandler
              ->getOccurrencesForDisplay($date_range['min'], $date_range['max']);
            foreach ($occurrences as $occurrence) {

              /** @var \DateTime $start */
              $start = $occurrence['value'];

              /** @var \DateTime $end */
              $end = $occurrence['end_value'];
              $event = $this
                ->prepareEvent($entity, $field, $index, $start, $end);
            }
            $isRecurring = TRUE;
          }
        }
        if ($isRecurring === TRUE) {

          // At this point, all DateRecur occurrences are merged into $rows
          // so we can continue adding date items with the next field.
          continue;
        }
      }

      // "datetime" and "daterange" fields or "date_recur" field (without
      // recurring date).
      foreach ($field['value'] as $index => $item) {

        // Start time is required!
        if (empty($item['raw']->value)) {
          continue;
        }
        $event_start = new DateTime();
        $event_start
          ->setTimestamp(strtotime($item['raw']->value));

        // Set event timezone override if configured in views.
        if (!empty($field['timezone_override'])) {
          $event_start
            ->setTimeZone(new DateTimeZone($field['timezone_override']));
        }
        $event_end = new DateTime();

        // By default, we use the start-time + 1 hour as end-time.
        $event_end
          ->setTimestamp($event_start
          ->getTimestamp() + 3600);

        // If the field has end_value, override default end-time.
        if (!empty($item['raw']->end_value)) {
          $event_end
            ->setTimestamp(strtotime($item['raw']->end_value));

          // Set event timezone override if configured in views.
          if (!empty($field['timezone_override'])) {
            $event_end
              ->setTimeZone(new DateTimeZone($field['timezone_override']));
          }
        }
        $event_start
          ->setTimestamp($event_start
          ->getTimestamp() + $event_start
          ->getOffset());
        $event_end
          ->setTimestamp($event_end
          ->getTimestamp() + $event_end
          ->getOffset());
        $event = $this
          ->prepareEvent($entity, $field, $index, $event_start, $event_end);
      }
    }
    if (!empty($event)) {
      $events[$delta] = $event;
    }
  }
  return $events;
}