View source
<?php
function date_range_formatter_field_formatter_info() {
$formatters = array(
'date_range_without_time' => array(
'label' => t('Date range (without time)'),
'field types' => array(
'date',
'datestamp',
'datetime',
),
'settings' => array(
'single' => 'd F Y',
'single_all_day' => 'd F Y',
'one_day' => 'd F Y',
'one_month' => 'd - {d} F Y',
'several_months' => 'd F - {d} {F} Y',
'several_years' => 'd F Y - {d} {F} {Y}',
),
),
);
return $formatters;
}
function date_range_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$formatter = $display['type'];
$settings = $display['settings'];
$form = array();
switch ($formatter) {
case 'date_range_without_time':
$form['single'] = array(
'#type' => 'textfield',
'#title' => t('Date format for single date'),
'#default_value' => $settings['single'],
);
$form['single_all_day'] = array(
'#type' => 'textfield',
'#title' => t('Date format for the single date if the date is "all day"'),
'#default_value' => isset($settings['single_all_day']) ? $settings['single_all_day'] : $settings['single'],
);
$form['one_day'] = array(
'#type' => 'textfield',
'#title' => t('Date format for the single day date range'),
'#default_value' => isset($settings['one_day']) ? $settings['one_day'] : 'd F Y',
);
$form['one_month'] = array(
'#type' => 'textfield',
'#title' => t('Date format for the single month date range'),
'#default_value' => $settings['one_month'],
);
$form['several_months'] = array(
'#type' => 'textfield',
'#title' => t('Date format for the single year date range'),
'#default_value' => $settings['several_months'],
);
$form['several_years'] = array(
'#type' => 'textfield',
'#title' => t('Date format for multiple years date range'),
'#default_value' => $settings['several_years'],
);
$form['help'] = array(
'#type' => 'markup',
'#markup' => t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array(
'@url' => 'http://php.net/manual/function.date.php',
)) . '<br />' . t('Use letters in braces for end date elements, for example, {d} means the day of the end date.') . '<br />' . t('These format values are translated, for example, t("d F Y") instead of "d F Y" will be used as the actual date format.'),
);
break;
}
return $form;
}
function date_range_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$formatter = $display['type'];
$summary = '';
switch ($formatter) {
case 'date_range_without_time':
$summary = t('Display date range using formats: @single, @single_all_day, @one_day, @one_month, @several_months, @several_years', array(
'@single' => $settings['single'],
'@single_all_day' => isset($settings['single_all_day']) ? $settings['single_all_day'] : $settings['single'],
'@one_day' => isset($settings['one_day']) ? $settings['one_day'] : 'd F Y',
'@one_month' => $settings['one_month'],
'@several_months' => $settings['several_months'],
'@several_years' => $settings['several_years'],
));
break;
}
return $summary;
}
function date_range_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
$formatter = $display['type'];
switch ($formatter) {
case 'date_range_without_time':
foreach ($items as $delta => $item) {
$date_item = date_formatter_process('format_interval', $entity_type, $entity, $field, $instance, $langcode, $item, $display);
if (empty($item['value2']) || $item['value'] == $item['value2']) {
$format = $settings['single'];
if (isset($settings['single_all_day'])) {
$date = date_formatter_process('date_default', $entity_type, $entity, $field, $instance, $langcode, $item, $display);
if (date_range_formatter_is_all_day($field, $instance, $date['value']['db']['object'])) {
$format = $settings['single_all_day'];
}
}
$date_str = format_date(strtotime($date_item['value']['formatted_iso']), 'custom', t($format));
$element[$delta] = array(
'#markup' => '<span class="date-display-single">' . filter_xss_admin($date_str) . '</span>',
);
}
else {
$date1 = strtotime($date_item['value']['formatted_iso']);
$date2 = strtotime($date_item['value2']['formatted_iso']);
$format = $settings['several_years'];
if (date('Y', $date1) === date('Y', $date2)) {
$format = $settings['several_months'];
}
if (date('m.Y', $date1) === date('m.Y', $date2)) {
$format = $settings['one_month'];
}
if (date('d.m.Y', $date1) === date('d.m.Y', $date2)) {
$format = isset($settings['one_day']) ? $settings['one_day'] : 'd F Y';
}
$date_str = format_date($date1, 'custom', preg_replace('/\\{([a-zA-Z])\\}/', '{\\\\$1}', t($format)));
$matches = array();
if (preg_match_all('/\\{([a-zA-Z])\\}/', $date_str, $matches)) {
foreach ($matches[1] as $match) {
$date_str = preg_replace('/\\{' . $match . '\\}/', format_date($date2, 'custom', $match), $date_str);
}
}
$element[$delta] = array(
'#markup' => '<span class="date-display-range">' . filter_xss_admin($date_str) . '</span>',
);
}
}
break;
}
return $element;
}
function date_range_formatter_is_all_day($field, $instance, $date1, $date2 = NULL) {
if (empty($date1) || !is_object($date1)) {
return FALSE;
}
elseif (!date_has_time($field['settings']['granularity'])) {
return TRUE;
}
if (empty($date2)) {
$date2 = $date1;
}
$granularity = date_granularity_precision($field['settings']['granularity']);
$increment = isset($instance['widget']['settings']['increment']) ? $instance['widget']['settings']['increment'] : 1;
return date_is_all_day(date_format($date1, DATE_FORMAT_DATETIME), date_format($date2, DATE_FORMAT_DATETIME), $granularity, $increment);
}