diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeTimestampWidget.php b/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php similarity index 73% rename from core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeTimestampWidget.php rename to core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php index f3e03fc..341d0d2 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeTimestampWidget.php +++ b/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php @@ -1,12 +1,14 @@ getPattern(); - $time_format = entity_load('date_format', 'html_time')->getPattern(); + $date_format = DateFormat::load('html_date')->getPattern(); + $time_format = DateFormat::load('html_time')->getPattern(); $default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : ''; $element['value'] = $element + array( '#type' => 'datetime', '#default_value' => $default_value, + '#date_year_range' => '1902:2037', ); - $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', array('%format' => datetime_format_example($date_format . ' ' . $time_format))); + $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', array('%format' => Datetime::formatExample($date_format . ' ' . $time_format))); return $element; } @@ -46,7 +49,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen */ public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { foreach ($values as &$item) { - $date = NULL; // @todo The structure is different whether access is denied or not, to // be fixed in https://www.drupal.org/node/2326533. if (isset($item['value']) && $item['value'] instanceof DrupalDateTime) { @@ -55,14 +57,11 @@ public function massageFormValues(array $values, array $form, FormStateInterface else if (isset($item['value']['object']) && $item['value']['object'] instanceof DrupalDateTime) { $date = $item['value']['object']; } - if (!$date || $date->hasErrors()) { - $item['value'] = NULL; - } else { - $item['value'] = $date->getTimestamp(); + $date = new DrupalDateTime(); } + $item['value'] = $date->getTimestamp(); } - return $values; } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php index 72f71a2..d355287 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php @@ -19,8 +19,18 @@ * label = @Translation("Timestamp"), * description = @Translation("An entity field containing a UNIX timestamp value."), * no_ui = TRUE, - * default_widget = "timestamp", * default_formatter = "timestamp", + * constraints = { + * "ComplexData" = { + * "value" = { + * "Range" = { + * "min" = "-2147483648", + * "max" = "2147483648", + * } + * } + * } + * } + * ) * ) */ class TimestampItem extends FieldItemBase { @@ -47,4 +57,5 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) ), ); } + } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/TimestampWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/TimestampWidget.php deleted file mode 100644 index e1af72c..0000000 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/TimestampWidget.php +++ /dev/null @@ -1,86 +0,0 @@ - FALSE - ) + parent::defaultSettings(); - } - - /** - * {@inheritdoc} - */ - public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $default_value = isset($items[$delta]->value) ? format_date($items[$delta]->value, 'custom', 'Y-m-d H:i:s O') : ''; - $element['value'] = $element + array( - '#type' => 'textfield', - '#default_value' => $default_value, - '#maxlength' => 25, - ); - - if (!empty($default_value)) { - $example_time = date_format(date_create($default_value), 'Y-m-d H:i:s O'); - $timezone = date_format(date_create($default_value), 'O'); - } - else { - $example_time = format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O'); - $timezone = format_date(REQUEST_TIME, 'custom', 'O'); - } - $element['value']['#description'] = $this->t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array('%time' => $example_time, '%timezone' => $timezone)); - - return $element; - } - - /** - * {@inheritdoc} - */ - public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { - foreach ($values as &$item) { - $value = trim($item['value']); - if (empty($value)) { - $value = $this->getSetting('use_request_time_on_empty') ? REQUEST_TIME : 0; - } - else { - $date = new DrupalDateTime($value); - if ($date->hasErrors()) { - $value = FALSE; - } - else { - $value = $date->getTimestamp(); - } - } - $item['value'] = $value; - } - - return $values; - } - -} diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index 7c3dbe0..8687ec6 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -10,9 +10,6 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\Core\Template\Attribute; -use Drupal\Core\Datetime\Element\Datetime; -use Drupal\node\NodeInterface; /** * Defines the timezone that dates should be stored in. @@ -138,15 +135,3 @@ function datetime_datelist_widget_validate(&$element, FormStateInterface $form_s function datetime_date_default_time($date) { $date->setTime(12, 0, 0); } - -/** - * Implements hook_entity_base_field_info_alter(). - */ -function datetime_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) { - if ($entity_type->id() == 'node') { - $fields['created']->setDisplayOptions('form', array( - 'type' => 'datetime_timestamp', - 'weight' => 0, - )); - } -} diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index 2b05fe3..81e38cf 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -414,11 +414,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { 'weight' => 0, )) ->setDisplayOptions('form', array( - 'type' => 'timestamp', + 'type' => 'datetime_timestamp', 'weight' => 0, - 'settings' => array( - 'use_request_time_on_empty' => TRUE, - ), )) ->setDisplayConfigurable('form', TRUE);