View source
<?php
function addtocalendar_help($path, $arg) {
switch ($path) {
case 'admin/help#addtocalendar':
$output = file_get_contents(drupal_get_path('module', 'addtocalendar') . '/README.md');
return $output;
}
}
function addtocalendar_field_formatter_info_alter(&$info) {
if (isset($info['date_default'])) {
$info['date_default']['settings'] += array(
'addtocalendar_show' => '',
'addtocalendar_settings' => '',
);
}
}
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']);
$settings_form['addtocalendar_show'] = array(
'#type' => 'checkbox',
'#title' => t('Show Add to Calendar'),
'#default_value' => $settings['addtocalendar_show'],
);
$settings_form['addtocalendar_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Add to Calendar Settings'),
'#states' => array(
"visible" => array(
"input[name*='addtocalendar_show']" => array(
"checked" => TRUE,
),
),
),
);
$settings_form['addtocalendar_settings']['style'] = array(
'#type' => 'select',
'#title' => t('Select Style'),
'#options' => array(
0 => t('No Styling'),
'blue' => t('Blue'),
'glow_orange' => t('Glow Orange'),
),
'#default_value' => !empty($settings['addtocalendar_settings']['style']) ? $settings['addtocalendar_settings']['style'] : 'blue',
);
$settings_form['addtocalendar_settings']['display_text'] = array(
'#type' => 'textfield',
'#title' => t('Display Text'),
'#default_value' => !empty($settings['addtocalendar_settings']['display_text']) ? $settings['addtocalendar_settings']['display_text'] : t('Add to Calender'),
);
$info = array(
'atc_title' => t('Title'),
'atc_description' => t('Description'),
'atc_location' => t('Location'),
'atc_organizer' => t('Organizer'),
'atc_organizer_email' => t('Organizer email'),
'atc_date_end' => t('End Date'),
);
$field_options = array();
foreach ($field_list as $id => $field_i) {
$field_options[$id] = $field_i['label'];
}
$field_options = array_merge(array(
'token' => t('Use Token/Static Content'),
), array(
'title' => t('Title'),
), $field_options);
$info = array(
'atc_title' => t('Title'),
'atc_description' => t('Description'),
'atc_location' => t('Location'),
'atc_organizer' => t('Organizer'),
'atc_organizer_email' => t('Organizer email'),
'atc_date_end' => t('End Date'),
);
foreach ($info as $id => $label) {
$settings_form['addtocalendar_settings'][$id]['field'] = array(
'#type' => 'select',
'#title' => $label . t(': field'),
'#options' => $field_options,
'#description' => t('Select field to be used as @label for calendar events', array(
'@label' => $label,
)),
'#default_value' => !empty($settings['addtocalendar_settings'][$id]['field']) ? $settings['addtocalendar_settings'][$id]['field'] : '',
);
$settings_form['addtocalendar_settings'][$id]['tokenized'] = array(
'#type' => 'textfield',
'#title' => $label . t(': Static/Tokenized Content'),
'#default_value' => !empty($settings['addtocalendar_settings'][$id]['tokenized']) ? $settings['addtocalendar_settings'][$id]['tokenized'] : '',
);
}
$settings_form['addtocalendar_settings']['atc_privacy'] = array(
'#type' => 'select',
'#title' => t('Privacy'),
'#options' => array(
'public' => t('Public'),
'private' => t('Private'),
),
'#description' => t('Use public for free access to event information from any places. User private if the event is closed to public access.'),
'#default_value' => !empty($settings['addtocalendar_settings']['atc_privacy']) ? $settings['addtocalendar_settings']['atc_privacy'] : '',
);
$settings_form['addtocalendar_settings']['data_secure'] = array(
'#type' => 'select',
'#title' => t('Security level'),
'#options' => array(
'auto' => t('Auto'),
'true' => t('Use https only'),
'false' => t('Use http only'),
),
'#default_value' => !empty($settings['addtocalendar_settings']['data_secure']) ? $settings['addtocalendar_settings']['data_secure'] : '',
);
$settings_form['addtocalendar_settings']['data_calendars'] = array(
'#type' => 'checkboxes',
'#options' => array(
'iCalendar' => t('iCalendar'),
'Google Calendar' => t('Google Calendar'),
'Outlook' => t('Outlook'),
'Outlook Online' => t('Outlook Online'),
'Yahoo! Calendar' => t('Yahoo! Calendar'),
),
'#default_value' => !empty($settings['addtocalendar_settings']['data_calendars']) ? $settings['addtocalendar_settings']['data_calendars'] : '',
'#title' => t('List of calendars to show in button list.'),
);
}
}
function addtocalendar_field_formatter_settings_summary_alter(&$summary, $context) {
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.');
}
}
}
function addtocalendar_preprocess_field(&$variables) {
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);
$variables['addtocalendar_settings'] = $formatter_settings['addtocalendar_settings'];
if (!empty($variables['addtocalendar_settings'])) {
$build['addtocalendar'] = array();
$settings = $variables['addtocalendar_settings'];
if ($formatter_settings['addtocalendar_show']) {
$timeZone = date_default_timezone_get();
$date = date('m/d/Y h:ia', strtotime($entity->{$field_name}['und'][0]['value']));
$build['addtocalendar']['atc_date_start'] = array(
'#type' => 'html_tag',
'#tag' => 'var',
'#value' => $date,
'#attributes' => array(
'class' => 'atc_date_start',
),
);
$info = array(
'atc_date_end',
'atc_title',
'atc_description',
'atc_location',
'atc_organizer',
'atc_organizer_email',
);
foreach ($info as $value) {
switch ($settings[$value]['field']) {
case 'token':
$class_value = $settings[$value]['tokenized'];
break;
case 'title':
$class_value = entity_label($entity_type, $entity);
break;
default:
$field = $settings[$value]['field'];
$field_data = field_get_items($entity_type, $entity, $field);
$class_value = $field_data[0]['value'];
break;
}
$build['addtocalendar'][$value] = array(
'#type' => 'html_tag',
'#tag' => 'var',
'#value' => $class_value,
'#attributes' => array(
'class' => $value,
),
);
}
$build['addtocalendar']['atc_date_end']['#value'] = !empty($build['addtocalendar']['atc_date_end']['#value']) ? $date = date('m/d/Y g:ia', strtotime($build['addtocalendar']['atc_date_end']['#value'])) : $date;
$build['addtocalendar']['atc_timezone'] = array(
'#type' => 'html_tag',
'#tag' => 'var',
'#value' => $timeZone,
'#attributes' => array(
'class' => 'atc_timezone',
),
);
$build['addtocalendar']['atc_privacy'] = array(
'#type' => 'html_tag',
'#tag' => 'var',
'#value' => $settings['atc_privacy'],
'#attributes' => array(
'class' => 'atc_privacy',
),
);
$build['addtocalendar'] = array(
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => '<var class="atc_event">' . render($build['addtocalendar']) . '</var>',
'#attributes' => array(
'class' => array(
'addtocalendar',
),
),
);
if ($settings['data_calendars']) {
$value = '';
foreach ($settings['data_calendars'] as $key => $set) {
if ($set) {
$value .= $key . ', ';
}
}
if ($value) {
$build['addtocalendar']['#attributes']['data-calendars'] = $value;
}
}
$build['addtocalendar']['#attributes']['data-secure'] = $settings['data_secure'];
switch ($settings['style']) {
case 'blue':
$style['class'] = 'atc-style-blue';
drupal_add_js('//addtocalendar.com/atc/1.5/atc.min.js', 'external');
drupal_add_css('//addtocalendar.com/atc/1.5/atc-style-blue.css', 'external');
break;
case 'glow_orange':
$style['class'] = 'atc-style-glow-orange';
drupal_add_js('//addtocalendar.com/atc/1.5/atc.min.js', 'external');
drupal_add_css('//addtocalendar.com/atc/1.5/atc-style-glow-orange.css', 'external');
break;
default:
drupal_add_js('//addtocalendar.com/atc/1.5/atc.min.js', 'external');
drupal_add_css('//addtocalendar.com/atc/1.5/atc-base.css', 'external');
break;
}
if (!empty($style)) {
$build['addtocalendar']['#attributes']['class'][] = $style['class'];
}
}
$variables['items'][0]['#markup'] .= render($build['addtocalendar']);
$variables['#attached']['library'][] = drupal_add_css('//addtocalendar.com/atc/1.5/atc-base.css', 'external');
}
}
}