diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateRangeItem.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateRangeItem.php index 6779d4f..3ab96ad 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateRangeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateRangeItem.php @@ -66,9 +66,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) $schema['columns']['end_value'] = [ 'description' => 'The end date value.', - 'type' => 'varchar', - 'length' => 20, - ]; + ] + $schema['columns']['value']; $schema['indexes']['end_value'] = ['end_value']; diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php index fc04de0..bd311d2 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php @@ -62,12 +62,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // Identify the type of date and time elements to use. switch ($this->getFieldSetting('datetime_type')) { case DateRangeItem::DATETIME_TYPE_DATE: - $date_type = 'date'; - $time_type = 'none'; - $date_format = $this->dateStorage->load('html_date')->getPattern(); - $time_format = ''; - break; - case DateRangeItem::DATETIME_TYPE_ALLDAY: $date_type = 'date'; $time_type = 'none'; diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php index 8842d0d..ce0964d 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php @@ -4,80 +4,36 @@ use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\datetime\Plugin\Field\FieldType\DateRangeItem; /** * Base class for the 'daterange_*' widgets. */ -class DateRangeWidgetBase extends WidgetBase { +class DateRangeWidgetBase extends DateTimeWidgetBase { /** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - // We are nesting some sub-elements inside the parent, so we need a wrapper. - // We also need to add another #title attribute at the top level for ease in - // identifying this item in error messages. We do not want to display this - // title because the actual title display is handled at a higher level by - // the Field module. - - $element['#theme_wrappers'][] = 'datetime_wrapper'; - $element['#attributes']['class'][] = 'container-inline'; + $element = parent::formElement($items, $delta, $element, $form, $form_state); $element['#element_validate'][] = [$this, 'validateStartEnd']; - - $element['value'] = [ - '#title' => $this->t('Start'), - '#type' => 'datetime', - '#default_value' => NULL, - '#date_increment' => 1, - '#date_timezone' => drupal_get_user_timezone(), - '#required' => $element['#required'], - ]; + $element['value']['#title'] = $this->t('Start'); $element['end_value'] = [ '#title' => $this->t('End'), - '#type' => 'datetime', - '#default_value' => NULL, - '#date_increment' => 1, - '#date_timezone' => drupal_get_user_timezone(), - '#required' => $element['#required'], - ]; - - if ($this->getFieldSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATE) { - // A date-only field should have no timezone conversion performed, so - // use the same timezone as for storage. - $element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE; - $element['end_value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE; - } + ] + $element['value']; if ($items[$delta]->start_date) { /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */ $start_date = $items[$delta]->start_date; - // The date was created and verified during field_load(), so it is safe to - // use without further inspection. - if ($this->getFieldSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATE) { - // A date without time will pick up the current time, use the default - // time. - datetime_date_default_time($start_date); - } - $start_date->setTimezone(new \DateTimeZone($element['value']['#date_timezone'])); - $element['value']['#default_value'] = $start_date; + $element['value']['#default_value'] = $this->createDefaultValue($start_date, $element['value']['#date_timezone']); } if ($items[$delta]->end_date) { - /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */ + /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */ $end_date = $items[$delta]->end_date; - // The date was created and verified during field_load(), so it is safe to - // use without further inspection. - if ($this->getFieldSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATE) { - // A date without time will pick up the current time, use the default - // time. - datetime_date_default_time($end_date); - } - $end_date->setTimezone(new \DateTimeZone($element['end_value']['#date_timezone'])); - $element['end_value']['#default_value'] = $end_date; + $element['end_value']['#default_value'] = $this->createDefaultValue($end_date, $element['end_value']['#date_timezone']); } return $element; @@ -103,7 +59,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface break; case DateRangeItem::DATETIME_TYPE_ALLDAY: - // All day field start at midnight on the starting date, but are + // All day fields 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. @@ -133,7 +89,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface break; case DateRangeItem::DATETIME_TYPE_ALLDAY: - // All day field end at midnight on the end date, but are + // All day fields 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. diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php index aa6175a..054d3e0 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php @@ -41,16 +41,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen } if ($items[$delta]->date) { + /** @var \Drupal\Core\Datetime\DrupalDateTime $date */ $date = $items[$delta]->date; - // The date was created and verified during field_load(), so it is safe to - // use without further inspection. - if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) { - // A date without time will pick up the current time, use the default - // time. - datetime_date_default_time($date); - } - $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; @@ -86,4 +79,30 @@ 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. + if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) { + // A date without time will pick up the current time, use the default + // time. + datetime_date_default_time($date); + } + $date->setTimezone(new \DateTimeZone($timezone)); + return $date; + } + }