You are here

date_ical.module in Date iCal 7

Same filename and directory in other branches
  1. 7.3 date_ical.module
  2. 7.2 date_ical.module

Adds ical functionality to Views.

File

date_ical.module
View source
<?php

/**
 * @file
 * Adds ical functionality to Views.
 */

/**
 * Implements hook_views_api().
 */
function date_ical_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'date_ical'),
  );
}

/**
 * Implements hook_theme().
 */
function date_ical_theme($existing, $type, $theme, $path) {
  return array(
    'date_ical_icon' => array(
      'variables' => array(
        'url' => NULL,
      ),
    ),
  );
}

/**
 * The theme for the ical icon.
 */
function theme_date_ical_icon($variables) {
  $url = $variables['url'];
  $variables = array(
    'path' => drupal_get_path('module', 'date_ical') . '/images/ical-feed-icon-34x14.png',
    'title' => t('Add to calendar'),
    'alt' => t('Add to calendar'),
  );
  if ($image = theme('image', $variables)) {
    return '<a href="' . check_url($url) . '" class="ical-icon" title="ical">' . $image . '</a>';
  }
}

/**
 * Implements hook_preprocess_HOOK() for nodes.
 *
 * Hide extraneous information when printing an ical view. The same thing can be
 * done in the theme for other entities, and this function can be overridden in
 * the theme to produce different results for nodes.
 */
function date_ical_preprocess_node(&$variables) {
  if (isset($variables['view_mode']) && $variables['view_mode'] == 'ical') {

    // We hide the page elements we won't want to see.
    // The display of the body and other fields will be controlled
    // by the Display Fields settings for the ical view mode.
    // Trick the default node template into not displaying the page title by telling it this is page.
    $variables['page'] = TRUE;
    $variables['title_prefix'] = '';
    $variables['title_suffix'] = '';

    // We probably don't want to see the author information in our feed.
    $variables['display_submitted'] = FALSE;

    // No comments in a feed.
    if (isset($variables['content']['comments'])) {
      unset($variables['content']['comments']);
    }

    // No links in a feed.
    if (isset($variables['content']['links'])) {
      unset($variables['content']['links']);
    }
  }
}

/**
 * Implements hook_entity_info_alter().
 *
 * Add an 'iCal' view mode for all entities, similar to RSS view mode.
 */
function date_ical_entity_info_alter(&$entity_info) {
  foreach ($entity_info as $entity_type => $info) {
    if (!isset($entity_info[$entity_type]['view modes'])) {
      $entity_info[$entity_type]['view modes'] = array();
    }
    $entity_info[$entity_type]['view modes'] += array(
      'ical' => array(
        'label' => t('iCal'),
        'custom settings' => TRUE,
      ),
    );
  }
}

/**
 * Implements hook_views_plugins().
 */
function date_ical_views_plugins() {
  $module_path = drupal_get_path('module', 'date_ical');
  $theme_path = $module_path . '/theme';
  $data = array(
    'module' => 'date_ical',
    // This just tells our themes are elsewhere.
    'style' => array(
      'date_ical' => array(
        'title' => t('Date iCal Feed'),
        'help' => t('Generates an iCal VCALENDAR feed from a view.'),
        'handler' => 'date_ical_plugin_style_ical_feed',
        'path' => $module_path,
        'theme' => 'date_ical_vcalendar',
        'theme file' => 'theme.inc',
        'theme path' => $theme_path,
        'uses fields' => TRUE,
        'uses grouping' => FALSE,
        'uses row plugin' => TRUE,
        'uses options' => TRUE,
        'type' => 'feed',
        'even empty' => TRUE,
      ),
    ),
    'row' => array(
      'date_ical' => array(
        'title' => t('Date iCal Entities'),
        'help' => t('Display each entity as an iCal VEVENT item.'),
        'handler' => 'date_ical_plugin_row_ical_feed',
        'path' => $module_path,
        'theme' => 'date_ical_vevent',
        'theme file' => 'theme.inc',
        'theme path' => $theme_path,
        'uses options' => TRUE,
        'uses fields' => FALSE,
        'type' => 'feed',
      ),
    ),
  );
  return $data;
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Technique borrowed from Display Suite module.
 * Add a custom preprocess hook that will work for all types of entities
 */
function date_ical_theme_registry_alter(&$theme_registry) {
  $entity_info = entity_get_info();
  foreach ($entity_info as $entity => $info) {

    // User uses user_profile for theming.
    if ($entity == 'user') {
      $entity = 'user_profile';
    }

    // Only add preprocess functions if entity exposes theme function.
    if (isset($theme_registry[$entity])) {
      $theme_registry[$entity]['preprocess functions'][] = 'date_ical_preprocess_date_ical';
    }
  }

  // Support for File Entity.
  if (isset($theme_registry['file_entity'])) {
    $theme_registry['file_entity']['preprocess functions'][] = 'date_ical_preprocess_date_ical';
  }

  // Support for Entity API.
  if (isset($theme_registry['entity'])) {
    $theme_registry['entity']['preprocess functions'][] = 'date_ical_preprocess_date_ical';
  }
}

/**
 * Technique borrowed from Display Suite module.
 * Add ical template suggestions to all types of entities.
 */
function date_ical_preprocess_date_ical(&$vars) {
  if (isset($vars['elements']) && isset($vars['elements']['#entity_type']) && isset($vars['elements']['#bundle']) && isset($vars['view_mode']) && $vars['view_mode'] == 'ical') {
    $vars['theme_hook_suggestions'][] = $vars['elements']['#entity_type'] . '__ical';
    $vars['theme_hook_suggestions'][] = $vars['elements']['#entity_type'] . '__' . $vars['elements']['#bundle'] . '__ical';
  }
}

Functions

Namesort descending Description
date_ical_entity_info_alter Implements hook_entity_info_alter().
date_ical_preprocess_date_ical Technique borrowed from Display Suite module. Add ical template suggestions to all types of entities.
date_ical_preprocess_node Implements hook_preprocess_HOOK() for nodes.
date_ical_theme Implements hook_theme().
date_ical_theme_registry_alter Implements hook_theme_registry_alter().
date_ical_views_api Implements hook_views_api().
date_ical_views_plugins Implements hook_views_plugins().
theme_date_ical_icon The theme for the ical icon.