diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index 103a8bb..522725e 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -51,7 +51,7 @@ function datetime_help($route_name, RouteMatchInterface $route_match) { * * @param $date * - * @deprecated in Drupal 8.3.0 and will be removed before Drupal 9.0.0. Use + * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use * (TBD) instead. */ function datetime_date_default_time($date) { diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php index c36f87f..1ea8241 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php @@ -44,8 +44,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $date = $items[$delta]->date; // The date was created and verified during field_load(), so it is safe to // use without further inspection. - $date->setTimezone(new \DateTimeZone($element['value']['#date_timezone'])); - $element['value']['#default_value'] = $date; + $element['value']['#default_value'] = $this->createDefaultValue($date, $element['value']['#date_timezone']); } return $element; @@ -78,4 +77,26 @@ public function massageFormValues(array $values, array $form, FormStateInterface return $values; } + /** + * Creates a date object for use as a default value. + * + * This will take a default value, apply the proper timezone for display in + * a widget, and set the default time for date-only fields. + * + * @param \Drupal\Core\Datetime\DrupalDateTime $date + * The UTC default date. + * @param string $timezone + * The timezone to apply. + * + * @return \Drupal\Core\Datetime\DrupalDateTime + * A date object for use as a default value in a field widget. + */ + protected function createDefaultValue($date, $timezone) { + // The date was created and verified during field_load(), so it is safe to + // use without further inspection. + $date->setTime(12, 0, 0); + $date->setTimezone(new \DateTimeZone($timezone)); + return $date; + } + } diff --git a/core/modules/datetime/src/Tests/DateTestBase.php b/core/modules/datetime/src/Tests/DateTestBase.php index 0f1277e..d24ca9f 100644 --- a/core/modules/datetime/src/Tests/DateTestBase.php +++ b/core/modules/datetime/src/Tests/DateTestBase.php @@ -3,6 +3,7 @@ namespace Drupal\datetime\Tests; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem; @@ -179,4 +180,19 @@ protected function setSiteTimezone($timezone) { ->save(); } + /** + * Massages test date values. + * + * If a date object is generated directly by a test, then it needs to be + * adjusted to behave like the computed date from the item. + * + * @param \Drupal\Core\Datetime\DrupalDateTime $date + * A date object directly generated by the test. + */ + protected function massageTestDate($date) { + // Mirror what \Drupal\datetime\DateTimeComputed::getValue() does to + // date-only items. + $date->setTime(12, 0, 0); + } + } diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index f7aeae8..7da5566 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -96,7 +96,7 @@ function testDateField() { // Formats that display a time component for date-only fields will display // the default time, so that is applied before calculating the expected // value. - datetime_date_default_time($date); + $this->massageTestDate($date); foreach ($options as $setting => $values) { foreach ($values as $new_value) { // Update the entity display settings. diff --git a/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php index 30f58af..5c8e271 100644 --- a/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php +++ b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php @@ -132,25 +132,4 @@ public function validateStartEnd(array &$element, FormStateInterface $form_state } } - /** - * Creates a date object for use as a default value. - * - * This will take a default value, apply the proper timezone for display in - * a widget, and set the default time for date-only fields. - * - * @param \Drupal\Core\Datetime\DrupalDateTime $date - * The UTC default date. - * @param string $timezone - * The timezone to apply. - * - * @return \Drupal\Core\Datetime\DrupalDateTime - * A date object for use as a default value in a field widget. - */ - protected function createDefaultValue($date, $timezone) { - // The date was created and verified during field_load(), so it is safe to - // use without further inspection. - $date->setTimezone(new \DateTimeZone($timezone)); - return $date; - } - } diff --git a/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php b/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php index 759352e..e5839f1 100644 --- a/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php +++ b/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php @@ -105,8 +105,8 @@ public function testDateRangeField() { // Formats that display a time component for date-only fields will display // the default time, so that is applied before calculating the expected // value. - datetime_date_default_time($start_date); - datetime_date_default_time($end_date); + $this->massageTestDate($start_date); + $this->massageTestDate($end_date); // Reset display options since these get changed below. $this->displayOptions = [ @@ -183,7 +183,7 @@ public function testDateRangeField() { $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); - datetime_date_default_time($start_date); + $this->massageTestDate($start_date); $this->displayOptions = [ 'type' => 'daterange_default',