From 15b2b5edc3d6799333da48bd3f13f946f1091fd9 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 22 Aug 2015 17:45:12 +0100 Subject: [PATCH] Issue #2120813 by pjonckiere, mpdonadio, ursula, borisson_, jsbalsera: Allow several date formats with identical date format strings --- .../system/src/Form/DateFormatFormBase.php | 7 +++---- .../system/src/Tests/System/DateTimeTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/modules/system/src/Form/DateFormatFormBase.php b/core/modules/system/src/Form/DateFormatFormBase.php index b8d0e1788252..bf78cc4642de 100644 --- a/core/modules/system/src/Form/DateFormatFormBase.php +++ b/core/modules/system/src/Form/DateFormatFormBase.php @@ -130,12 +130,11 @@ abstract class DateFormatFormBase extends EntityForm { parent::validateForm($form, $form_state); // The machine name field should already check to see if the requested - // machine name is available. Regardless of machine_name or human readable - // name, check to see if the provided pattern exists. + // machine name is available. $pattern = trim($form_state->getValue('date_format_pattern')); foreach ($this->dateFormatStorage->loadMultiple() as $format) { - if ($format->getPattern() == $pattern && ($this->entity->isNew() || $format->id() != $this->entity->id())) { - $form_state->setErrorByName('date_format_pattern', $this->t('This format already exists. Enter a unique format string.')); + if ($format->getPattern() == $pattern && ($format->id() == $this->entity->id())) { + drupal_set_message(t('The existing format/name combination has not been altered.')); continue; } } diff --git a/core/modules/system/src/Tests/System/DateTimeTest.php b/core/modules/system/src/Tests/System/DateTimeTest.php index 88868e995525..4fdc2790fa22 100644 --- a/core/modules/system/src/Tests/System/DateTimeTest.php +++ b/core/modules/system/src/Tests/System/DateTimeTest.php @@ -117,6 +117,21 @@ class DateTimeTest extends WebTestBase { // Make sure the date does not exist in config. $date_format = entity_load('date_format', $date_format_id); $this->assertFalse($date_format); + + // Add a new date format with an existing format. + $date_format_id = strtolower($this->randomMachineName(8)); + $name = ucwords($date_format_id); + $date_format = 'Y'; + $edit = array( + 'id' => $date_format_id, + 'label' => $name, + 'date_format_pattern' => $date_format, + ); + $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format')); + $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.'); + $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.'); + $this->assertText($name, 'Custom date format appears in the date format list.'); + $this->assertText(t('Delete'), 'Delete link for custom date format appears.'); } /**