Commit 3d9ac252 authored by catch's avatar catch

Issue #3087606 by jhedstrom, kim.pepper, jonathan1055, partdigital, larowlan,...

Issue #3087606 by jhedstrom, kim.pepper, jonathan1055, partdigital, larowlan, acbramley, Berdir, yunke: Datetime::getInfo() caches user's timezone causing unpredictable timestamps
parent d0d8e87b
......@@ -38,6 +38,9 @@ public function getInfo() {
}
$class = get_class($this);
// Note that since this information is cached, the #date_timezone property
// is not set here, as this needs to vary potentially by-user.
return [
'#input' => TRUE,
'#element_validate' => [
......@@ -61,7 +64,6 @@ public function getInfo() {
'#date_time_callbacks' => [],
'#date_year_range' => '1900:2050',
'#date_increment' => 1,
'#date_timezone' => date_default_timezone_get(),
];
}
......@@ -69,6 +71,8 @@ public function getInfo() {
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$element += ['#date_timezone' => date_default_timezone_get()];
if ($input !== FALSE) {
$date_input = $element['#date_date_element'] != 'none' && !empty($input['date']) ? $input['date'] : '';
$time_input = $element['#date_time_element'] != 'none' && !empty($input['time']) ? $input['time'] : '';
......
......@@ -58,8 +58,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => 'datelist test',
'#type' => 'datetime',
'#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
'#date_date_format' => ['Y-m-d'],
'#date_time_format' => ['H:i:s'],
'#date_date_format' => 'Y-m-d',
'#date_time_format' => 'H:i:s',
'#date_date_element' => 'HTML Date',
'#date_time_element' => 'HTML Time',
'#date_increment' => 1,
......@@ -69,8 +69,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// Element without specifying the default value.
$form['simple_datetime_element'] = [
'#type' => 'datetime',
'#date_date_format' => ['Y-m-d'],
'#date_time_format' => ['H:i:s'],
'#date_date_format' => 'Y-m-d',
'#date_time_format' => 'H:i:s',
'#date_date_element' => 'HTML Date',
'#date_time_element' => 'HTML Time',
];
......@@ -108,4 +108,21 @@ public function testDatetimeElement() {
$this->assertEqual(t('Date time callback called.'), $this->flag);
}
/**
* Tests proper timezone handling of the Datetime element.
*/
public function testTimezoneHandling() {
// Render the form once with the site's timezone.
$form = \Drupal::formBuilder()->getForm($this);
$this->render($form);
$this->assertEquals('Australia/Sydney', $form['datetime_element']['#date_timezone']);
// Mimic a user with a different timezone than Australia/Sydney.
date_default_timezone_set('UTC');
$form = \Drupal::formBuilder()->getForm($this);
$this->render($form);
$this->assertEquals('UTC', $form['datetime_element']['#date_timezone']);
}
}
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