You are here

private function FullCalendar::prepareEvent in FullCalendar 8.3

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

Helper method to prepare event.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Event entity.

$field:

int $delta: Field delta.

\DateTime $event_start: Start date of the event.

\DateTime $event_end: End date of the event.

Return value

array

Throws

\Exception

1 call to FullCalendar::prepareEvent()
FullCalendar::prepareEvents in src/Plugin/views/style/FullCalendar.php
Prepare events for calendar.

File

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

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

private function prepareEvent($entity, $field, $delta, $event_start, $event_end) {
  $classes = $this->moduleHandler
    ->invokeAll('fullcalendar_classes', [
    $entity,
  ]);
  $this->moduleHandler
    ->alter('fullcalendar_classes', $classes, $entity);
  $classes = array_map([
    '\\Drupal\\Component\\Utility\\Html',
    'getClass',
  ], $classes);
  $class = count($classes) ? implode(' ', array_unique($classes)) : '';
  $palette = $this->moduleHandler
    ->invokeAll('fullcalendar_palette', [
    $entity,
  ]);
  $this->moduleHandler
    ->alter('fullcalendar_palette', $palette, $entity);
  $request_time = \Drupal::time()
    ->getRequestTime();
  $current_time = new DateTime();
  $current_time
    ->setTimestamp($request_time);

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

  // All-day option (for agenda views) is FALSE by default.
  $all_day = FALSE;

  // If the event starts before and ends after the current date-range
  // appears in the Calendar, set event to all-day event.
  if ($event_start <= $date_range['min'] && $event_end >= $date_range['max']) {
    $all_day = TRUE;
  }

  // Add a class if the event was in the past or is in the future, based
  // on the end time. We can't do this in hook_fullcalendar_classes()
  // because the date hasn't been processed yet.
  if ($all_day && $event_start < $current_time || !$all_day && $event_end < $current_time) {
    $time_class = 'fc-event-past';
  }
  elseif ($event_start > $current_time) {
    $time_class = 'fc-event-future';
  }
  else {
    $time_class = 'fc-event-now';
  }
  if (!empty($settings['fullcalendar']['editable'])) {
    $editable = $entity
      ->access('update', NULL, TRUE)
      ->isAllowed();
  }
  else {
    $editable = FALSE;
  }
  return [
    'allDay' => (int) $all_day,
    'start' => $this->dateFormatter
      ->format($event_start
      ->getTimestamp(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    'end' => $this->dateFormatter
      ->format($event_end
      ->getTimestamp(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    'editable' => !empty($editable) ? 'true' : 'false',
    'field' => $field['field_name'],
    'index' => $delta,
    'eid' => $entity
      ->id(),
    'entity_type' => $entity
      ->getEntityTypeId(),
    'className' => $class . ' ' . $time_class,
    'title' => strip_tags(htmlspecialchars_decode($entity
      ->label(), ENT_QUOTES)),
    'url' => $entity
      ->toUrl('canonical', [
      'language' => \Drupal::languageManager()
        ->getCurrentLanguage(),
    ])
      ->toString(),
    'backgroundColor' => !empty($palette['backgroundColor']) ? $palette['backgroundColor'] : '',
    'borderColor' => !empty($palette['borderColor']) ? $palette['borderColor'] : '',
    'textColor' => !empty($palette['textColor']) ? $palette['textColor'] : '',
  ];
}