You are here

protected function FullCalendar::prepareEvents in FullCalendar 8.4

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 src/Plugin/views/style/FullCalendar.php \Drupal\fullcalendar\Plugin\views\style\FullCalendar::prepareEvents()
  3. 8.2 src/Plugin/views/style/FullCalendar.php \Drupal\fullcalendar\Plugin\views\style\FullCalendar::prepareEvents()
  4. 8.3 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 298

Class

FullCalendar
Plugin annotation @ViewsStyle( id = "fullcalendar", title = @Translation("FullCalendar"), help = @Translation("Displays items on a calendar."), theme = "views_view--fullcalendar", 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\EntityField $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']];
        $values = $field
          ->getItems($row);
        if (!empty($values)) {
          $date_fields[$field_name] = [
            'value' => $values,
            'field_alias' => $field->field_alias,
            'field_name' => $field_definition
              ->getName(),
            'field_info' => $field_definition,
            'timezone_override' => $field->options['settings']['timezone_override'],
          ];
        }
      }
    }

    // @todo: custom date field?
    // 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, 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);
            }
            $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 = $this
          ->prepareEvent($entity, $date_fields, $index);
      }
    }
    if (!empty($event)) {
      $events[$delta] = $event;
    }
  }
  return $events;
}