You are here

function template_preprocess_calendar in Calendar 7

Same name in this branch
  1. 7 theme/theme.inc \template_preprocess_calendar()
  2. 7 calendar_multiday/theme/theme.inc \template_preprocess_calendar()
Same name and namespace in other branches
  1. 6.2 theme/theme.inc \template_preprocess_calendar()
  2. 6.2 calendar_multiday/theme/theme.inc \template_preprocess_calendar()
  3. 7.2 theme/theme.inc \template_preprocess_calendar()
  4. 7.2 calendar_multiday/theme/theme.inc \template_preprocess_calendar()

Display a view as a calendar.

This preprocessor does all the work needed for all types of calendar views and the template takes care of displaying links to related views.

8 calls to template_preprocess_calendar()
template_preprocess_calendar_day in theme/theme.inc
Display a day view.
template_preprocess_calendar_day in calendar_multiday/theme/theme.inc
Display a day view.
template_preprocess_calendar_month in theme/theme.inc
Display a month view.
template_preprocess_calendar_month in calendar_multiday/theme/theme.inc
Display a month view.
template_preprocess_calendar_week in theme/theme.inc
Display a week view.

... See full list

File

theme/theme.inc, line 103
Theme functions for the Calendar module.

Code

function template_preprocess_calendar(&$vars) {
  module_load_include('inc', 'calendar', 'includes/calendar');
  $view = $vars['view'];

  // Make sure we only run through this function one time.
  if (!empty($view->date_info->calendar_processed)) {
    return;
  }
  $result = (array) $view->result;
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
  $fields = $view->field;

  // Render each field into an output array. We have to do the rendering
  // here because we don't apppear to have full access to the view
  // handlers in the theme functions.
  $items = array();
  $calendar_fields = date_views_fields($view->base_table);
  $calendar_fields = array_keys($calendar_fields['alias']);

  // Try to figure out the 'id' for this collection of items.
  // The id field is often not a field but instead an 'additional field',
  // so this is cludgy.
  foreach ($result as $num => $row) {
    $keys = array_keys((array) $row);
    foreach ($keys as $key) {
      if (strlen($key) == 3 && substr($key, -2) == 'id' && !empty($row->{$key})) {
        $id = $key;
      }
    }
  }
  foreach ($result as $num => $row) {
    $copy = clone $row;
    $items[$num] = $row;
    $items[$num]->raw = $copy;
    $items[$num]->calendar_fields = new stdClass();
    $items[$num]->id = !empty($row->{$id}) ? $row->{$id} : NULL;
    foreach ($row as $key => $value) {
      if (in_array($key, $calendar_fields)) {
        $items[$num]->calendar_fields->{$key} = $value;
      }
    }
    foreach ($fields as $name => $field) {

      // Some fields, like the node edit and delete links, have no alias.
      $field_alias = $field->field_alias != 'unknown' ? $field->field_alias : $name;
      if ($field->real_field == 'entity_id' || $field->real_field == 'revision_id') {
        $items[$num]->id = $row->{$field_alias};

        //$col = $field->definition['field_name'] . '_' . $field->options['group_column'];

        //$field_alias = $field->aliases[$col];
      }
      if (!empty($field) && is_object($field)) {

        // Theme the copy instead of the original row so duplicate date
        // fields each get a fresh copy of the original data to theme.
        $items[$num]->{$field_alias} = $field
          ->theme($copy);
      }
      if (!empty($field->options['exclude'])) {
        if (isset($items[$num]->{$field_alias})) {
          unset($items[$num]->{$field_alias});
        }
      }
    }
  }
  $vars['display_type'] = $view->date_info->granularity;
  $vars['min_date_formatted'] = date_format($view->date_info->min_date, DATE_FORMAT_DATETIME);
  $vars['max_date_formatted'] = date_format($view->date_info->max_date, DATE_FORMAT_DATETIME);

  // Massage the resulting items into formatted calendar items.
  $items = calendar_build_nodes($view, $items);

  // Merge in items from other sources.
  foreach (module_implements('calendar_add_items') as $module) {
    $function = $module . '_calendar_add_items';
    if (function_exists($function)) {
      if ($feeds = $function($view)) {
        foreach ($feeds as $feed) {
          $items = $feed;
        }
      }
    }
  }
  $view->date_info->mini = isset($view->date_info->mini) ? $view->date_info->mini : $view->date_info->granularity == 'year';

  // Create the calendar day names and rows.
  $rows = calendar_build_calendar($view, $items);
  $vars['items'] = $items;
  $vars['rows'] = $rows;
  $view->date_info->calendar_processed = TRUE;
  $vars['view'] = $view;
  $vars['mini'] = !empty($view->date_info->mini);
  $vars['block'] = !empty($view->date_info->block);
}