You are here

function fullcalendar_prepare_events in FullCalendar 7.2

Build a render array representing the events.

Parameters

object $view: The view object.

array $rows: An array of row objects.

array $options: An array of options from the style plugin.

Return value

array A render array of events.

1 call to fullcalendar_prepare_events()
template_preprocess_fullcalendar in theme/theme.inc
Builds the FullCalendar structure as a render array.

File

theme/theme.inc, line 130
Preprocess functions for FullCalendar.

Code

function fullcalendar_prepare_events($view, $rows, $options) {
  if (empty($rows)) {
    return;
  }
  $events = array();
  foreach ($rows as $delta => $row) {

    // Collect all fields for the customize options.
    $fields = array();

    // Collect only date fields.
    $date_fields = array();
    foreach ($view->field as $field_name => $field) {
      $fields[$field_name] = $view->style_plugin
        ->get_field($delta, $field_name);
      if (fullcalendar_field_is_date($field)) {
        $date_fields[$field_name] = array(
          'value' => $field
            ->get_items($row),
          'field_alias' => $field->field_alias,
          'field_name' => $field->field_info['field_name'],
          'field_info' => $field->field_info,
        );
      }
    }

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

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

    // This should never happen, but just in case.
    if (!isset($row->_field_data)) {
      return $events;
    }
    $entities = array();
    $event = array();
    foreach ($date_fields as $field) {

      // If this row doesn't contain this entity, or if this entity has already
      // been processed, skip it.
      if (!isset($row->_field_data[$field['field_alias']])) {
        continue;
      }
      if (!isset($entities[$field['field_alias']])) {

        // Find the field's alias that refers to it's entity.
        $alias = $field['field_alias'];
        $entity = $row->_field_data[$alias]['entity'];
        $entity->entity_type = $row->_field_data[$alias]['entity_type'];
        list(, , $bundle) = entity_extract_ids($entity->entity_type, $entity);
        $entity->bundle = $bundle;
        $entity->eid = $row->{$alias};
        $entity->options = $view->style_options;

        // If the view disallows editing, that's it.
        if (!empty($view->fullcalendar_disallow_editable)) {
          $entity->editable = FALSE;
        }
        else {

          // Allow resize/drag/drop of an event if user has proper permissions.
          $editable = module_invoke_all('fullcalendar_editable', $entity, $view);

          // If one value is FALSE, return FALSE. The identical operator is needed
          // because of the return value of array_search().
          $editable = array_search(FALSE, $editable, TRUE) === FALSE;
          drupal_alter('fullcalendar_editable', $editable, $entity, $view);
          $entity->editable = $editable;
        }

        // Store the current date field name for later.
        $entity->fullcalendar_date_field = $field['field_name'];

        // Create a string of valid HTML class names and add them to the entity.
        $classes = module_invoke_all('fullcalendar_classes', $entity);
        drupal_alter('fullcalendar_classes', $classes, $entity);
        $classes = array_map('drupal_html_class', $classes);
        $entity->class = implode(' ', array_unique($classes));

        // Default URL.
        $uri = entity_uri($entity->entity_type, $entity);
        $entity->url = isset($uri['path']) ? $uri['path'] : '';

        // Fetch custom URL if needed.
        if (!empty($options['url'])) {
          $field_name = $options['url_field'];
          if (!empty($fields[$field_name])) {
            $entity->url = ltrim($fields[$field_name], '/');
          }
        }

        // Fetch custom title if needed.
        if (!isset($entity->title)) {
          $entity->title = '';
        }
        if (!empty($options['title'])) {
          $field_name = $options['title_field'];
          if (!empty($fields[$field_name])) {
            $entity->title = $fields[$field_name];
          }
        }
        $entities[$alias] = $entity;
      }
      $entity = $entities[$field['field_alias']];

      // Filter fields without value.
      if (!empty($field['value'])) {
        $instance = field_info_instance($entity->entity_type, $field['field_name'], $bundle);
        foreach ($field['value'] as $index => $item) {
          $dates = _fullcalendar_process_dates($instance, $entity, $field['field_info'], $item['raw']);
          if (empty($dates)) {
            continue;
          }
          list($start, $end, $all_day) = $dates;

          // 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 && strtotime($start) < strtotime('today') || !$all_day && strtotime($end) < REQUEST_TIME) {
            $time_class = 'fc-event-past';
          }
          elseif (strtotime($start) > REQUEST_TIME) {
            $time_class = 'fc-event-future';
          }
          else {
            $time_class = 'fc-event-now';
          }
          $event[] = array(
            '#theme' => 'link',
            '#text' => $item['rendered']['#markup'],
            '#path' => $entity->url,
            '#options' => array(
              'attributes' => array(
                'data-all-day' => $all_day,
                'data-start' => $start,
                'data-end' => $end,
                'data-editable' => $entity->editable,
                'data-field' => $field['field_name'],
                'data-index' => $index,
                'data-eid' => $entity->eid,
                'data-entity-type' => $entity->entity_type,
                'data-cn' => $entity->class . ' ' . $time_class,
                'title' => strip_tags(htmlspecialchars_decode($entity->title, ENT_QUOTES)),
                'class' => array(
                  'fullcalendar-event-details',
                ),
              ),
              'html' => TRUE,
            ),
          );
        }
      }
    }
    if (!empty($event)) {
      $events[$delta] = array(
        '#theme' => 'fullcalendar_event',
        '#event' => $event,
        '#entity' => isset($entities[$view->base_field]) ? $entities[$view->base_field] : reset($entities),
      );
    }
  }
  return $events;
}