From fe7c0ff88a54bc1e46be8afd6e543f30de5a8977 Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Sat, 20 Jul 2024 09:32:13 +0900
Subject: [PATCH] Issue #3399970 by markconroy, catch, smustgrave: Umami
 content is all created in the same second

---
 .../demo_umami_content.install                | 53 +++++++++++++++++++
 .../UmamiMultilingualInstallTest.php          |  2 +-
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install b/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
index db02670cd236..f3df9bfc6e1f 100644
--- a/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
+++ b/core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
@@ -18,6 +18,59 @@ function demo_umami_content_module_preinstall($module) {
   }
 }
 
+/**
+ * Implements hook_install().
+ */
+function demo_umami_content_install($is_syncing) {
+  if (!$is_syncing) {
+    $query = \Drupal::entityQuery('node')->accessCheck(TRUE);
+    $nids = $query->execute();
+
+    if (empty($nids)) {
+      return;
+    }
+
+    // Sort node IDs in descending order
+    rsort($nids);
+
+    $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
+
+    // Use a counter to determine the "days ago" offset
+    $counter = 1;
+
+    foreach ($nodes as $node) {
+      // Get all translation languages for the node
+      $languages = $node->getTranslationLanguages();
+      foreach ($languages as $language) {
+        // Load the translated version of the node.
+        $translatedNode = $node->getTranslation($language->getId());
+        try {
+          // Use the counter to calculate "days ago".
+          $timestamp = strtotime("-{$counter} days");
+
+          // Set the created and updated time for the translated node.
+          $translatedNode->setCreatedTime($timestamp);
+          $translatedNode->setChangedTime($timestamp);
+
+          // Check if the translation is the default language.
+          // If not, mark the translation as changed.
+          if (!$translatedNode->isDefaultTranslation()) {
+            $translatedNode->setNewRevision(FALSE);
+            $translatedNode->isDefaultRevision(FALSE);
+            $translatedNode->setRevisionTranslationAffected(TRUE);
+          }
+          $translatedNode->save();
+        }
+        catch (\Exception $e) {
+          \Drupal::logger('system')->error($e->getMessage());
+        }
+      }
+      // Increment the counter for the next node
+      $counter++;
+    }
+  }
+}
+
 /**
  * Implements hook_uninstall().
  */
diff --git a/core/profiles/demo_umami/tests/src/Functional/UmamiMultilingualInstallTest.php b/core/profiles/demo_umami/tests/src/Functional/UmamiMultilingualInstallTest.php
index b01f7d68c1ba..30ef88da32a9 100644
--- a/core/profiles/demo_umami/tests/src/Functional/UmamiMultilingualInstallTest.php
+++ b/core/profiles/demo_umami/tests/src/Functional/UmamiMultilingualInstallTest.php
@@ -29,7 +29,7 @@ class UmamiMultilingualInstallTest extends InstallerTestBase {
   public function testUmami(): void {
     $this->drupalGet('');
     // cSpell:disable-next-line
-    $this->assertSession()->pageTextContains('Quiche mediterráneo profundo');
+    $this->assertSession()->pageTextContains('Crema catalana');
   }
 
   /**
-- 
GitLab