Commit 5731bc7d authored by plach's avatar plach

Issue #2799987 by jonathanshaw, oriol_e9g, Jo Fitzgerald, mpdonadio, GoZ,...

Issue #2799987 by jonathanshaw, oriol_e9g, Jo Fitzgerald, mpdonadio, GoZ, Mixologic, harsha012, jhedstrom: Datetime and Datelist element's timezone handling is fragile, buggy and wrongly documented
parent c028d851
......@@ -33,7 +33,7 @@ public function getInfo() {
'#date_year_range' => '1900:2050',
'#date_increment' => 1,
'#date_date_callbacks' => [],
'#date_timezone' => '',
'#date_timezone' => drupal_get_user_timezone(),
];
}
......@@ -60,9 +60,8 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
}
unset($input['ampm']);
}
$timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
try {
$date = DrupalDateTime::createFromArray($input, $timezone);
$date = DrupalDateTime::createFromArray($input, $element['#date_timezone']);
}
catch (\Exception $e) {
$form_state->setError($element, t('Selected combination of day and month is not valid.'));
......@@ -77,6 +76,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
if (!empty($element['#default_value'])) {
$date = $element['#default_value'];
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
$date->setTimezone(new \DateTimeZone($element['#date_timezone']));
static::incrementRound($date, $increment);
foreach ($parts as $part) {
switch ($part) {
......@@ -147,12 +147,8 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
* - #date_increment: The increment to use for minutes and seconds, i.e.
* '15' would show only :00, :15, :30 and :45. Defaults to 1 to show every
* minute.
* - #date_timezone: The local timezone to use when creating dates. Generally
* this should be left empty and it will be set correctly for the user using
* the form. Useful if the default value is empty to designate a desired
* timezone for dates created in form processing. If a default date is
* provided, this value will be ignored, the timezone in the default date
* takes precedence. Defaults to the value returned by
* - #date_timezone: The local timezone to use when displaying or
* interpreting dates. Defaults to the value returned by
* drupal_get_user_timezone().
*
* Example usage:
......@@ -183,17 +179,6 @@ public static function processDatelist(&$element, FormStateInterface $form_state
// The value callback has populated the #value array.
$date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
// Set a fallback timezone.
if ($date instanceof DrupalDateTime) {
$element['#date_timezone'] = $date->getTimezone()->getName();
}
elseif (!empty($element['#timezone'])) {
$element['#date_timezone'] = $element['#date_timezone'];
}
else {
$element['#date_timezone'] = drupal_get_user_timezone();
}
$element['#tree'] = TRUE;
// Determine the order of the date elements.
......
......@@ -61,7 +61,7 @@ public function getInfo() {
'#date_time_callbacks' => [],
'#date_year_range' => '1900:2050',
'#date_increment' => 1,
'#date_timezone' => '',
'#date_timezone' => drupal_get_user_timezone(),
];
}
......@@ -74,7 +74,6 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
$time_input = $element['#date_time_element'] != 'none' && !empty($input['time']) ? $input['time'] : '';
$date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
$time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
$timezone = !empty($element['#date_timezone']) ? $element['#date_timezone'] : NULL;
// Seconds will be omitted in a post in case there's no entry.
if (!empty($time_input) && strlen($time_input) == 5) {
......@@ -84,7 +83,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
try {
$date_time_format = trim($date_format . ' ' . $time_format);
$date_time_input = trim($date_input . ' ' . $time_input);
$date = DrupalDateTime::createFromFormat($date_time_format, $date_time_input, $timezone);
$date = DrupalDateTime::createFromFormat($date_time_format, $date_time_input, $element['#date_timezone']);
}
catch (\Exception $e) {
$date = NULL;
......@@ -98,6 +97,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
else {
$date = isset($element['#default_value']) ? $element['#default_value'] : NULL;
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
$date->setTimezone(new \DateTimeZone($element['#date_timezone']));
$input = [
'date' => $date->format($element['#date_date_format']),
'time' => $date->format($element['#date_time_format']),
......@@ -188,12 +188,8 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
* "seconds"-component will not be shown in the input. Used for HTML5 step
* values and jQueryUI datepicker settings. Defaults to 1 to show every
* second.
* - #date_timezone: The local timezone to use when creating dates. Generally
* this should be left empty and it will be set correctly for the user using
* the form. Useful if the default value is empty to designate a desired
* timezone for dates created in form processing. If a default date is
* provided, this value will be ignored, the timezone in the default date
* takes precedence. Defaults to the value returned by
* - #date_timezone: The local timezone to use when displaying or
* interpreting dates. Defaults to the value returned by
* drupal_get_user_timezone().
*
* Example usage:
......@@ -222,14 +218,6 @@ public static function processDatetime(&$element, FormStateInterface $form_state
// The value callback has populated the #value array.
$date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
// Set a fallback timezone.
if ($date instanceof DrupalDateTime) {
$element['#date_timezone'] = $date->getTimezone()->getName();
}
elseif (empty($element['#timezone'])) {
$element['#date_timezone'] = drupal_get_user_timezone();
}
$element['#tree'] = TRUE;
if ($element['#date_date_element'] != 'none') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment