From 2ded516747be7d776cc5d4b1a98a93d98a5013da Mon Sep 17 00:00:00 2001
From: Igor Biki <ibiki@uwaterloo.ca>
Date: Tue, 25 Mar 2025 14:05:34 -0400
Subject: [PATCH] ISTWCMS-7275: Refactor node storage access and add temp
 storage updates.

Simplified node storage handling by reusing the storage object and removed redundant calls. Added functionality to update temporary storage, including processing layout sections and updating labels for specific components. Adjusted batch end message to include temporary storage updates.
---
 uw_custom_blocks.post_update.php | 69 +++++++++++++++++++++++++++-----
 1 file changed, 59 insertions(+), 10 deletions(-)

diff --git a/uw_custom_blocks.post_update.php b/uw_custom_blocks.post_update.php
index a1d73fcb..a4375102 100644
--- a/uw_custom_blocks.post_update.php
+++ b/uw_custom_blocks.post_update.php
@@ -60,6 +60,7 @@ function _references_block_titles_updates_batch_start($count, &$context): void {
   $context['results']['nodes_updated'] = 0;
   $context['results']['revisions_processed'] = 0;
   $context['results']['revisions_updated'] = 0;
+  $context['results']['temp_storage_updated'] = 0;
 
   $context['message'] = t("Processing @count nodes and each node revisions.", ['@count' => $count]);
 }
@@ -78,7 +79,8 @@ function _references_block_titles_updates_batch_start($count, &$context): void {
  *   including the number of nodes and revisions processed and updated.
  */
 function _references_block_titles_updates_batch_update($nid, &$context): void {
-  $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
+  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
+  $node = $node_storage->load($nid);
 
   if ($node) {
     $node->setSyncing(TRUE);
@@ -103,20 +105,16 @@ function _references_block_titles_updates_batch_update($nid, &$context): void {
     }
 
     if ($save_needed) {
-      // $node->save();
+      $node->save();
       $context['results']['nodes_updated']++;
     }
 
     $context['results']['nodes_processed']++;
 
-    $revisions = \Drupal::entityTypeManager()
-      ->getStorage('node')
-      ->revisionIds($node);
+    $revisions = $node_storage->revisionIds($node);
 
     foreach ($revisions as $revision) {
-      $revision = \Drupal::entityTypeManager()
-        ->getStorage('node')
-        ->loadRevision($revision);
+      $revision = $node_storage->loadRevision($revision);
 
       if ($revision) {
         $revision->setSyncing(TRUE);
@@ -141,7 +139,7 @@ function _references_block_titles_updates_batch_update($nid, &$context): void {
         }
 
         if ($revision_save_needed) {
-          // $revision->save();
+          $revision->save();
           $context['results']['revisions_updated']++;
         }
         $context['results']['revisions_processed']++;
@@ -160,6 +158,53 @@ function _references_block_titles_updates_batch_update($nid, &$context): void {
 function _references_block_titles_updates_batch_temp_storage(&$context) {
   // Make sure to load temp storage, and loop over sections there.
   $context['message'] = t('Processing temporary storage.');
+
+  // The DB connection.
+  $database = \Drupal::database();
+
+  // Query to get the layout builder from nodes.
+  $query = $database->select('key_value_expire', 'kv');
+  $query->fields('kv');
+  $query->condition('kv.collection', '%layout%', 'LIKE');
+  $result = $query->execute();
+
+  // Step through the results and change out any sections.
+  foreach ($result as $record) {
+    $save_needed = FALSE;
+
+    // Deserialize the value, which has the sections in it.
+    $value = unserialize($record->value);
+
+    // Get the section storage from the serialized data.
+    $layout = $value->data['section_storage'];
+
+    // Get out the sections.
+    foreach ($layout->getSections() as &$section) {
+      // Step through each of the components.
+      foreach ($section?->getComponents() as $component) {
+        if ($component?->getPluginId() == 'uw_cbl_publication_search') {
+          $configuration = $component->get('configuration');
+
+          if ($configuration['label'] == 'Publication reference search') {
+            $configuration['label'] = 'Reference search';
+            $component->set('configuration', $configuration);
+            $save_needed = TRUE;
+          }
+        }
+      }
+    }
+
+    if ($save_needed) {
+      $value->data['section_storage'] = $layout;
+
+      $database->update('key_value_expire')
+        ->fields(['value' => serialize($value)])
+        ->condition('name', $record->name)
+        ->execute();
+
+      $context['results']['temp_storage_updated']++;
+    }
+  }
 }
 
 /**
@@ -170,7 +215,7 @@ function _references_block_titles_updates_batch_temp_storage(&$context) {
  *   about nodes and revisions processed during the batch operation.
  */
 function _references_block_titles_updates_batch_end(&$context) {
-  $context['message'] = t('Successfully updated @nodes_updated/@nodes_processed.', [
+  $context['message'] = t('Successfully updated @nodes_updated/@nodes_processed. nodes', [
     '@nodes_updated' => $context['results']['nodes_updated'],
     '@nodes_processed' => $context['results']['nodes_processed'],
   ]);
@@ -178,4 +223,8 @@ function _references_block_titles_updates_batch_end(&$context) {
     '@revisions_updated' => $context['results']['revisions_updated'],
     '@revisions_processed' => $context['results']['revisions_processed'],
   ]);
+
+  $context['message'] = t('Successfully updated @temp_storage_updated temporary storage.', [
+    '@temp_storage_updated' => $context['results']['temp_storage_updated'],
+  ]);
 }
-- 
GitLab