You are here

addtocalendar.module in Add To Calendar Button (AddEvent.com) 7.2

Contains module code.

File

addtocalendar.module
View source
<?php

/**
 * @file
 * Contains module code.
 */

/**
 * Implements hook_help().
 */
function addtocalendar_help($path, $arg) {
  switch ($path) {
    case 'admin/help#addtocalendar':
      $output = file_get_contents(drupal_get_path('module', 'addtocalendar') . '/README.md');
      return $output;
  }
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function addtocalendar_field_formatter_info_alter(&$info) {

  // Add a setting to a formatter type.
  if (isset($info['date_default'])) {
    $info['date_default']['settings'] += array(
      'addtocalendar_show' => '',
      'addtocalendar_settings' => '',
    );
  }
}

/**
 * Implements hook_field_formatter_settings_form_alter().
 */
function addtocalendar_field_formatter_settings_form_alter(&$settings_form, $context) {
  if ($context['instance']['display'][$context['view_mode']]['type'] == 'date_default') {
    $display = $context['instance']['display'][$context['view_mode']];
    $settings = $display['settings'];
    $field_list = field_info_instances($context['form']['#entity_type'], $context['form']['#bundle']);

    // Add settings form.
    module_load_include('inc', 'addtocalendar', 'includes/addtocalendar');
    $settings_form = addtocalendar_settings_form($settings, $field_list);
  }
}

/**
 * Implements hook_field_formatter_settings_summary_alter().
 */
function addtocalendar_field_formatter_settings_summary_alter(&$summary, $context) {

  // Append a message to the summary.
  if ($context['instance']['display'][$context['view_mode']]['type'] == 'date_default') {
    $display = $context['instance']['display'][$context['view_mode']];
    $settings = $display['settings'];
    if (!empty($summary)) {
      $summary .= '';
    }
    if ($settings['addtocalendar_show']) {
      $summary .= '</br>' . t('Add to calendar is enabled.');
    }
    else {
      $summary .= '</br>' . t('Add to calendar is disabled.');
    }
  }
}

/**
 * Implements hook_preprocess_field().
 */
function addtocalendar_preprocess_field(&$variables) {

  // Provide an extra variable to the field template when the field uses
  // a formatter of type 'foo_formatter'.
  if ($variables['element']['#formatter'] == 'date_default') {
    $entity_type = $variables['element']['#entity_type'];
    $field_name = $variables['element']['#field_name'];
    $bundle = $variables['element']['#bundle'];
    $view_mode = $variables['element']['#view_mode'];
    $entity = $variables['element']['#object'];
    $formatter_settings = field_formatter_settings_get_instance_display_settings($entity_type, $field_name, $bundle, $view_mode);

    // Make the setting available in the field template.
    $variables['addtocalendar_settings'] = $formatter_settings['addtocalendar_settings'];
    if (!empty($variables['addtocalendar_settings'])) {
      $build['addtocalendar'] = array();
      $settings = $variables['addtocalendar_settings'];
      $settings['atc_date_start'] = array(
        'field' => $field_name,
        'tokenized' => date('Y-m-d H:i:s', time()),
      );
      module_load_include('inc', 'addtocalendar', 'includes/addtocalendar');
      $build = addtocalendar_preprocess_setting_fields($entity_type, $entity, $settings, $formatter_settings);
      $variables['items'][0]['#markup'] .= render($build['addtocalendar']);
      $variables['#attached']['library'][] = drupal_add_css('//addtocalendar.com/atc/1.5/atc-base.css', 'external');
    }
  }
}

/**
 * Display time as per timezone.
 */
function _addtocalendar_convert_to_local_time($date, $timezone) {
  $datetime = new DateTime($date, new DateTimeZone('UTC'));
  $datetime
    ->setTimezone(new DateTimeZone($timezone));
  return $datetime
    ->format('Y-m-d H:i:s');
}

/**
 * Implements hook_field_info().
 */
function addtocalendar_field_info() {
  return array(
    'addtocalendar' => array(
      'label' => t('Add to calendar'),
      'description' => t('Add to calendar field type.'),
      'default_widget' => 'addtocalendar_widget',
      'default_formatter' => 'addtocalendar_formatter',
    ),
  );
}

/**
 * Implements hook_field_formatter_info().
 */
function addtocalendar_field_formatter_info() {
  return array(
    'addtocalendar_formatter' => array(
      'label' => t('Add to calendar'),
      'field types' => array(
        'addtocalendar',
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_info().
 */
function addtocalendar_field_widget_info() {
  return array(
    'addtocalendar_widget' => array(
      'label' => t('Add to calendar widget'),
      'field types' => array(
        'addtocalendar',
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function addtocalendar_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($field['cardinality'] == 1) {
    $element['#type'] = 'fieldset';
  }
  $element['display_addtocalendar'] = array(
    '#title' => t('Show add to calendar widget'),
    '#type' => 'checkbox',
    '#default_value' => isset($items[$delta]['display_addtocalendar']) ? $items[$delta]['display_addtocalendar'] : '',
  );
  return $element;
}

/**
 * Implements hook_field_widget_settings_form().
 */
function addtocalendar_field_widget_settings_form($field, $instance) {
  $element = [];
  if ($instance['display']['default']['type'] == 'addtocalendar_formatter') {
    $settings = $instance['widget']['settings'];
    $settings['widget_setting_form'] = TRUE;
    $field_list = field_info_instances($instance['entity_type'], $instance['bundle']);

    // Add settings form.
    module_load_include('inc', 'addtocalendar', 'includes/addtocalendar');
    $element = addtocalendar_settings_form($settings, $field_list);
  }
  return $element;
}

/**
 * Implements hook_field_is_empty().
 */
function addtocalendar_field_is_empty($item, $field) {
  if (empty($item['display_addtocalendar'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_field_validate().
 */
function addtocalendar_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $key => $item) {
    if (!empty($item['display_addtocalendar'])) {

      // Make sure it is available.
      if (empty($item['display_addtocalendar'])) {
        $errors[$field['field_name']][$langcode][$key][] = array(
          'error' => 'addtocalendar_check',
          'message' => t('Some error in Add to calendar'),
        );
      }
    }
  }
}

/**
 * Implements hook_field_formatter_view().
 */
function addtocalendar_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if ($display['type'] == 'addtocalendar_formatter') {
    $settings = $instance['widget']['settings']['addtocalendar_settings'];

    // Start building renderable array.
    if (isset($items[0])) {
      $formatter_settings['addtocalendar_show'] = TRUE;
      $build = [];
      module_load_include('inc', 'addtocalendar', 'includes/addtocalendar');
      foreach ($items as $delta => $item) {
        $build = addtocalendar_preprocess_setting_fields($entity_type, $entity, $settings, $formatter_settings);
      }
      $element[]['#markup'] = !empty($build['addtocalendar']) ? render($build['addtocalendar']) : '';
    }
    else {
      $element = array(
        '#prefix' => '<div class="wrapper">',
        '#suffix' => '<div>' . $settings['disable_text'] . '</div></div>',
      );
    }
  }
  return $element;
}