From 53e541da1410f91ac84887f60ce0f5d7d1864349 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 3 May 2016 11:25:37 +0100
Subject: [PATCH] Issue #2716991 by mpdonadio: Incorrect ISO datetime attribute
 for date-only fields w/ default formatter

---
 .../Field/FieldFormatter/DateTimeDefaultFormatter.php       | 6 ++++--
 core/modules/datetime/src/Tests/DateTimeFieldTest.php       | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
index cbeb73bc18b8..3fac2524c7ab 100644
--- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
@@ -41,13 +41,15 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
       if ($item->date) {
         /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
         $date = $item->date;
-        // Create the ISO date in Universal Time.
-        $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
 
         if ($this->getFieldSetting('datetime_type') == 'date') {
           // A date without time will pick up the current time, use the default.
           datetime_date_default_time($date);
         }
+
+        // Create the ISO date in Universal Time.
+        $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
+
         $this->setTimeZone($date);
 
         $output = $this->formatDate($date);
diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php
index 1fc1d6096693..bd9e3af38984 100644
--- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php
+++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php
@@ -162,8 +162,9 @@ function testDateField() {
           case 'format_type':
             // Verify that a date is displayed.
             $expected = format_date($date->getTimestamp(), $new_value);
+            $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
             $this->renderTestEntity($id);
-            $this->assertText($expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected.', array('%value' => $new_value, '%expected' => $expected)));
+            $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso)));
             break;
         }
       }
@@ -291,8 +292,9 @@ function testDatetimeField() {
           case 'format_type':
             // Verify that a date is displayed.
             $expected = format_date($date->getTimestamp(), $new_value);
+            $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
             $this->renderTestEntity($id);
-            $this->assertText($expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected.', array('%value' => $new_value, '%expected' => $expected)));
+            $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso)));
             break;
         }
       }
-- 
GitLab