Skip to content
Snippets Groups Projects
Commit a5adf1c5 authored by Tim Plunkett's avatar Tim Plunkett Committed by Tim Plunkett
Browse files

Issue #1273946 by Dmitriy.trt, tim.plunkett | ralf.strobel: Added Option to...

Issue #1273946 by Dmitriy.trt, tim.plunkett | ralf.strobel: Added Option to override user time zone in date handlers.
parent 1189d6cc
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -16,6 +16,7 @@ function option_definition() {
$options['date_format'] = array('default' => 'small');
$options['custom_date_format'] = array('default' => '');
$options['timezone'] = array('default' => '');
return $options;
}
......@@ -55,6 +56,18 @@ function options_form(&$form, &$form_state) {
':input[name="options[date_format]"]' => array('value' => $custom_date_possible),
);
}
$form['timezone'] = array(
'#type' => 'select',
'#title' => t('Timezone'),
'#description' => t('Timezone to be used for date output.'),
'#options' => array('' => t('- Default site/user timezone -')) + system_time_zones(FALSE),
'#default_value' => $this->options['timezone'],
);
foreach (array_merge(array('custom'), array_keys($date_formats)) as $timezone_date_formats) {
$form['timezone']['#states']['visible'][] = array(
':input[name="options[date_format]"]' => array('value' => $timezone_date_formats),
);
}
parent::options_form($form, $form_state);
}
......@@ -67,6 +80,7 @@ function render($values) {
}
if ($value) {
$timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
$time_diff = REQUEST_TIME - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
switch ($format) {
case 'raw time ago':
......@@ -85,11 +99,11 @@ function render($values) {
return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2)));
case 'custom':
if ($custom_format == 'r') {
return format_date($value, $format, $custom_format, null, 'en');
return format_date($value, $format, $custom_format, $timezone, 'en');
}
return format_date($value, $format, $custom_format);
return format_date($value, $format, $custom_format, $timezone);
default:
return format_date($value, $format);
return format_date($value, $format, '', $timezone);
}
}
}
......
......@@ -42,25 +42,50 @@ public function testFieldDate() {
));
$time = gmmktime(0, 0, 0, 1, 1, 2000);
$maps = array(
'small' => format_date($time, 'small'),
'medium' => format_date($time, 'medium'),
'large' => format_date($time, 'large'),
'custom' => format_date($time, 'custom', 'c'),
$this->executeView($view);
$timezones = array(
NULL,
'UTC',
'America/New_York',
);
foreach ($timezones as $timezone) {
$dates = array(
'small' => format_date($time, 'small', '', $timezone),
'medium' => format_date($time, 'medium', '', $timezone),
'large' => format_date($time, 'large', '', $timezone),
'custom' => format_date($time, 'custom', 'c', $timezone),
);
$this->assertRenderedDatesEqual($view, $dates, $timezone);
}
$intervals = array(
'raw time ago' => format_interval(REQUEST_TIME - $time, 2),
'time ago' => t('%time ago', array('%time' => format_interval(REQUEST_TIME - $time, 2))),
// TODO write tests for them
// 'raw time span' => format_interval(REQUEST_TIME - $time, 2),
// 'time span' => t('%time hence', array('%time' => format_interval(REQUEST_TIME - $time, 2))),
);
$this->assertRenderedDatesEqual($view, $intervals);
}
$this->executeView($view);
foreach ($maps as $date_format => $expected_result) {
protected function assertRenderedDatesEqual($view, $map, $timezone = NULL) {
foreach ($map as $date_format => $expected_result) {
$view->field['created']->options['date_format'] = $date_format;
$this->assertEqual($expected_result, $view->field['created']->advanced_render($view->result[0]));
debug($view->field['created']->advanced_render($view->result[0]));
$t_args = array(
'%value' => $expected_result,
'%format' => $date_format,
);
if (isset($timezone)) {
$t_args['%timezone'] = $timezone;
$message = t('Value %value in %format format for timezone %timezone matches.', $t_args);
$view->field['created']->options['timezone'] = $timezone;
}
else {
$message = t('Value %value in %format format matches.', $t_args);
}
$actual_result = $view->field['created']->advanced_render($view->result[0]);
$this->assertEqual($expected_result, $actual_result, $message);
}
debug($view->result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment