diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php index 9a83720565..d54e24e5e4 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php @@ -5,8 +5,8 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Variable; use Drupal\Core\Datetime\DrupalDateTime; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Datetime\Entity\DateFormat; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Security\DoTrustedCallbackTrait; use Drupal\Core\Security\StaticTrustedCallbackHelper; @@ -239,7 +239,6 @@ public static function processDatetime(&$element, FormStateInterface $form_state // placeholders are invalid for HTML5 date and datetime, so an example // format is appended to the title to appear in tooltips. $extra_attributes = [ - 'title' => t('Date (e.g. @format)', ['@format' => static::formatExample($date_format)]), 'type' => $element['#date_date_element'], ]; @@ -285,7 +284,6 @@ public static function processDatetime(&$element, FormStateInterface $form_state // Adds the HTML5 attributes. $extra_attributes = [ - 'title' => t('Time (e.g. @format)', ['@format' => static::formatExample($time_format)]), 'type' => $element['#date_time_element'], 'step' => $element['#date_increment'], ]; @@ -352,9 +350,6 @@ public static function validateDatetime(&$element, FormStateInterface $form_stat if ($input_exists) { $title = static::getElementTitle($element, $complete_form); - $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : ''; - $time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : ''; - $format = trim($date_format . ' ' . $time_format); // If there's empty input and the field is not required, set it to empty. if (empty($input['date']) && empty($input['time']) && !$element['#required']) { @@ -363,7 +358,7 @@ public static function validateDatetime(&$element, FormStateInterface $form_stat // If there's empty input and the field is required, set an error. A // reminder of the required format in the message provides a good UX. elseif (empty($input['date']) && empty($input['time']) && $element['#required']) { - $form_state->setError($element, t('The %field date is required. Enter a date in the format %format.', ['%field' => $title, '%format' => static::formatExample($format)])); + $form_state->setError($element, t('The %field date is required.', ['%field' => $title])); } else { // If the date is valid, set it. @@ -374,29 +369,12 @@ public static function validateDatetime(&$element, FormStateInterface $form_stat // If the date is invalid, set an error. A reminder of the required // format in the message provides a good UX. else { - $form_state->setError($element, t('The %field date is invalid. Enter a date in the format %format.', ['%field' => $title, '%format' => static::formatExample($format)])); + $form_state->setError($element, t('The %field date is invalid. Enter a date in the correct format.', ['%field' => $title])); } } } } - /** - * Creates an example for a date format. - * - * This is centralized for a consistent method of creating these examples. - * - * @param string $format - * The date format. - * - * @return string - */ - public static function formatExample($format) { - if (!static::$dateExample) { - static::$dateExample = new DrupalDateTime(); - } - return static::$dateExample->format($format); - } - /** * Retrieves the right format for an HTML5 date element. * diff --git a/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php b/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php index 0ebe7b52d5..af3fd0dcee 100644 --- a/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php +++ b/core/lib/Drupal/Core/Datetime/Plugin/Field/FieldWidget/TimestampDatetimeWidget.php @@ -3,8 +3,6 @@ namespace Drupal\Core\Datetime\Plugin\Field\FieldWidget; use Drupal\Core\Datetime\DrupalDateTime; -use Drupal\Core\Datetime\Element\Datetime; -use Drupal\Core\Datetime\Entity\DateFormat; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; @@ -27,9 +25,8 @@ class TimestampDatetimeWidget extends WidgetBase { * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $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) : ''; + $parent_entity = $items->getParent()->getValue(); + $default_value = (!$parent_entity->isNew() && isset($items[$delta]->value)) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : ''; $element['value'] = $element + [ '#type' => 'datetime', '#default_value' => $default_value, @@ -38,9 +35,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $element['value']['#description'] = $element['#description'] !== '' ? $element['#description'] - : $this->t('Format: %format. Leave blank to use the time of form submission.', - ['%format' => Datetime::formatExample($date_format . ' ' . $time_format)]); - + : $this->t('Leave blank to use the time of form submission.'); return $element; } diff --git a/core/modules/content_translation/src/ContentTranslationHandler.php b/core/modules/content_translation/src/ContentTranslationHandler.php index d3dad8ee80..b4911b60af 100644 --- a/core/modules/content_translation/src/ContentTranslationHandler.php +++ b/core/modules/content_translation/src/ContentTranslationHandler.php @@ -541,10 +541,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En '#type' => 'textfield', '#title' => t('Authored on'), '#maxlength' => 25, - '#description' => 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.', [ - '%time' => $this->dateFormatter->format(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O'), - '%timezone' => $this->dateFormatter->format(REQUEST_TIME, 'custom', 'O'), - ]), + '#description' => t('Leave blank to use the time of form submission.'), '#default_value' => $new_translation || !$date ? '' : $this->dateFormatter->format($date, 'custom', 'Y-m-d H:i:s O'), ]; diff --git a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php index 797cdc6803..7dd0eaed5c 100644 --- a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php +++ b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php @@ -393,12 +393,9 @@ public function testDatetimeField() { $form_state = new FormState(); \Drupal::formBuilder()->submitForm($form, $form_state); $errors = $form_state->getErrors(); - $arguments = $errors["{$field_name}][0][value"]->getArguments(); - $expected_error_message = new FormattableMarkup('The %field date is required. Enter a date in the format %format.', ['%field' => $field_label, '%format' => $arguments['%format']]); + $expected_error_message = new FormattableMarkup('The %field date is required.', ['%field' => $field_label]); $actual_error_message = $errors["{$field_name}][0][value"]->__toString(); $this->assertEquals($expected_error_message->__toString(), $actual_error_message); - // Verify the format value is in the "YYYY-MM-DD HH:MM:SS" format. - $this->assertMatchesRegularExpression('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $arguments['%format']); } /** diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon index a3b1a56779..7920222b19 100644 --- a/core/phpstan-baseline.neon +++ b/core/phpstan-baseline.neon @@ -957,7 +957,7 @@ parameters: - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 5 + count: 3 path: modules/content_translation/src/ContentTranslationHandler.php -