diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php index 418f7f5..08d635e 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php @@ -98,6 +98,9 @@ public function massageFormValues(array $values, array $form, FormStateInterface case DateRangeItem::DATERANGE_TYPE_ALLDAY: // All day field start at midnight on the starting date, but are // stored like datetime fields, so we need to adjust the time. + // This function is called twice, so to prevent a double conversion + // we need to explicitly set the timezone. + $start_date->setTimeZone(timezone_open(drupal_get_user_timezone())); $start_date->setTime(0, 0, 0); $format = DATETIME_DATETIME_STORAGE_FORMAT; break; @@ -125,6 +128,9 @@ public function massageFormValues(array $values, array $form, FormStateInterface case DateRangeItem::DATERANGE_TYPE_ALLDAY: // All day field end at midnight on the end date, but are // stored like datetime fields, so we need to adjust the time. + // This function is called twice, so to prevent a double conversion + // we need to explicitly set the timezone. + $end_date->setTimeZone(timezone_open(drupal_get_user_timezone())); $end_date->setTime(23, 59, 59); $format = DATETIME_DATETIME_STORAGE_FORMAT; break; diff --git a/core/modules/datetime/src/Tests/DateRangeFieldTest.php b/core/modules/datetime/src/Tests/DateRangeFieldTest.php index 08cbc8f..556a6fa 100644 --- a/core/modules/datetime/src/Tests/DateRangeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateRangeFieldTest.php @@ -407,7 +407,7 @@ function testDatelistWidget() { ], ]) ->save(); - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Display creation form. $this->drupalGet('entity_test/add'); @@ -440,7 +440,7 @@ function testDatelistWidget() { ], ]) ->save(); - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Display creation form. $this->drupalGet('entity_test/add'); @@ -475,7 +475,7 @@ function testDatelistWidget() { ], ]) ->save(); - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Go to the form display page to assert that increment option does appear on Date Time $fieldEditUrl = 'entity_test/structure/entity_test/form-display'; @@ -573,7 +573,7 @@ function testDatelistWidget() { ], ]) ->save(); - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Display creation form. $this->drupalGet('entity_test/add'); @@ -626,7 +626,7 @@ function testDatelistWidget() { ], ]) ->save(); - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Test the widget for validation notifications. foreach ($this->datelistDataProvider() as $data) { @@ -798,7 +798,7 @@ function testDefaultValue() { ], 'Default value has been stored successfully'); // Clear field cache in order to avoid stale cache values. - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Create a new node to check that datetime field default value is today. $new_node = Node::create(['type' => 'date_content']); @@ -851,7 +851,7 @@ function testDefaultValue() { ], 'Default value has been stored successfully'); // Clear field cache in order to avoid stale cache values. - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Create a new node to check that datetime field default value is +90 days. $new_node = Node::create(['type' => 'date_content']); @@ -879,7 +879,7 @@ function testDefaultValue() { $this->assertTrue(empty($config_entity['default_value']), 'Empty default value has been stored successfully'); // Clear field cache in order to avoid stale cache values. - \Drupal::entityManager()->clearCachedFieldDefinitions(); + \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions(); // Create a new node to check that datetime field default value is not set. $new_node = Node::create(['type' => 'date_content']); @@ -1065,6 +1065,24 @@ function testInvalidField() { ]; $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', format_string('Invalid end second value %time has been caught.', ['%time' => $time_value])); + + $edit = [ + "{$field_name}[0][value][date]" => '2012-12-01', + "{$field_name}[0][value][time]" => '12:00:00', + "{$field_name}[0][value2][date]" => '2010-12-01', + "{$field_name}[0][value2][time]" => '12:00:00', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + $this->assertText('Start date should be equal to, or before, end date', 'End date before start date has been caught.'); + + $edit = [ + "{$field_name}[0][value][date]" => '2012-12-01', + "{$field_name}[0][value][time]" => '12:00:00', + "{$field_name}[0][value2][date]" => '2012-12-01', + "{$field_name}[0][value2][time]" => '11:00:00', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + $this->assertText('Start date should be equal to, or before, end date', 'End time before start time has been caught.'); } /**