From 97bc5ed7020b8ec04aa3e99db649cfa3118b835d Mon Sep 17 00:00:00 2001
From: Igor Biki <ibiki@uwaterloo.ca>
Date: Tue, 25 Mar 2025 11:23:51 -0400
Subject: [PATCH] ISTWCMS-7275: Refactor batch process for updating block
 titles.

Simplified and standardized function names and operations handling for batch processing. Improved context tracking and modularity, and added detailed documentation for better maintainability.
---
 uw_custom_blocks.post_update.php | 153 +++++++++++++++++++++----------
 1 file changed, 104 insertions(+), 49 deletions(-)

diff --git a/uw_custom_blocks.post_update.php b/uw_custom_blocks.post_update.php
index 1998b40a..2afcb8ce 100644
--- a/uw_custom_blocks.post_update.php
+++ b/uw_custom_blocks.post_update.php
@@ -8,66 +8,81 @@
 /**
  * Updating the title for "references" layout builder blocks titles.
  */
-function uw_custom_blocks_post_update_references_block_titles_updates(&$sandbox) {
-  // Load all nodes. Search for one block (publications reference search), and
-  // update the title is the title is default. Also use setSyncing(TRUE).
-
-  // Repeat the same for the revisions. Make sure to set setSyncing(TRUE)
-  // not to create any additional revisions.
-
-  // Repeat the same for temporary storage.
-
-  // Has to use batching, for all!!!! Maybe splitting it to 3 (node, rev, temp).
-
-  // Extrapolate common functionality into a separate function to process
-  // sections/components.
-
-  //  $storage = \Drupal::entityTypeManager()->getStorage('node');
-  //  $query = $storage->getQuery();
+function uw_custom_blocks_post_update_references_block_titles_updates_12(&$sandbox) {
   $query = \Drupal::entityQuery('node');
-  $all_nodes = $query->accessCheck(FALSE)->sort('nid', 'DESC')->execute();
+  $all_nodes = $query->accessCheck(FALSE)->sort('nid')->execute();
   $count = count($all_nodes);
   $chunks = array_chunk($all_nodes, 1);
 
-  // Load all revisions for node.
-
   $operations[] = [
-    '_uw_custom_blocks_post_update_references_block_titles_updates_batch_start',
-    $count,
+    '_references_block_titles_updates_batch_start',
+    [$count],
   ];
 
   foreach ($chunks as $chunk) {
     $operations[] = [
-      '_uw_custom_blocks_post_update_references_block_titles_updates_operation',
+      '_references_block_titles_updates_batch_update',
       $chunk,
     ];
   }
 
   $operations[] = [
-    '_uw_custom_blocks_post_update_references_block_titles_updates_batch_temp_storage',
+    '_references_block_titles_updates_batch_temp_storage',
+    [],
+  ];
+
+  $operations[] = [
+    '_references_block_titles_updates_batch_end',
+    [],
   ];
 
   $batch = [
     'title' => t('Updating block titles.'),
     'progressive' => TRUE,
     'operations' => $operations,
-    'finished' => '_uw_custom_blocks_post_update_references_block_titles_updates_finished',
+    'finished' => '_references_block_titles_updates_batch_finished',
   ];
 
   batch_set($batch);
 }
 
-function _uw_custom_blocks_post_update_references_block_titles_updates_batch_start($count, &$context): void {
-  $context['results']['total'] = $count;
-  $context['results']['processed'] = 0;
-  $context['results']['updated'] = 0;
+/**
+ * Initializes the batch process context for nodes and revisions updates.
+ *
+ * @param int $count
+ *   The total number of nodes to process.
+ * @param array $context
+ *   The batch context array, which is updated with initial counts and a status
+ *   message for the batch process.
+ */
+function _references_block_titles_updates_batch_start($count, &$context): void {
+  $context['results']['nodes_count'] = $count;
+  $context['results']['nodes_processed'] = 0;
+  $context['results']['nodes_updated'] = 0;
+  $context['results']['revisions_processed'] = 0;
+  $context['results']['revisions_updated'] = 0;
+
+  $context['message'] = t("Processing @count nodes and each node revisions.", ['@count' => $count]);
 }
 
-function _uw_custom_blocks_post_update_references_block_titles_updates_operation($nid, &$context): void {
-  if (!isset($context['results']['processed'])) {
-    $context['results']['processed'] = 0;
-  }
-
+/**
+ * Processes a single node and its revisions to update layout components.
+ *
+ * This function updates the label of specific layout components in both the
+ * node and its revisions. If changes are made, the node or revision is marked
+ * for saving, and the appropriate counters in the batch context are updated.
+ *
+ * @param int $nid
+ *   The node ID of the entity to be processed.
+ * @param array $context
+ *   A reference to the batch context array that tracks progress and statistics,
+ *   including the number of nodes and revisions processed and updated.
+ *
+ * @return void
+ *   This function does not return a value. It modifies the $context array
+ *   directly and updates the node and its revisions in-place.
+ */
+function _references_block_titles_updates_batch_update($nid, &$context): void {
   $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
 
   if ($node) {
@@ -93,14 +108,20 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation
     }
 
     if ($save_needed) {
-      $node->save();
-      $context['results']['updated']++;
+      // $node->save();
+      $context['results']['nodes_updated']++;
     }
 
-    $revisions = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);;
+    $context['results']['nodes_processed']++;
+
+    $revisions = \Drupal::entityTypeManager()
+      ->getStorage('node')
+      ->revisionIds($node);
 
     foreach ($revisions as $revision) {
-      $revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision);
+      $revision = \Drupal::entityTypeManager()
+        ->getStorage('node')
+        ->loadRevision($revision);
 
       if ($revision) {
         $revision->setSyncing(TRUE);
@@ -125,25 +146,59 @@ function _uw_custom_blocks_post_update_references_block_titles_updates_operation
         }
 
         if ($revision_save_needed) {
-          $revision->save();
-          $context['results']['updated']++;
+          // $revision->save();
+          $context['results']['revisions_updated']++;
         }
-        $context['results']['processed']++;
+        $context['results']['revisions_processed']++;
       }
-
     }
-
-    $context['results']['processed']++;
   }
-
-
 }
 
-function _uw_custom_blocks_post_update_references_block_titles_updates_batch_temp_storage(&$context) {
-
+/**
+ * Processes temporary storage during the batch operation.
+ *
+ * @param array $context
+ *   The batch context array, which is updated with a status message
+ *   indicating the processing of temporary storage.
+ */
+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.');
 }
 
-function _uw_custom_blocks_post_update_references_block_titles_updates_finished($success, $results) {
-  $dev = 'halt';
+/**
+ * Updates the batch end context messages for nodes and revisions.
+ *
+ * @param array $context
+ *   The batch context array, which gets updated with status messages
+ *   about nodes and revisions processed during the batch operation.
+ */
+function _references_block_titles_updates_batch_end(&$context) {
+  $context['message'] = t('Updated @nodes_updated out of @nodes_processed (total @total) nodes.', [
+    '@nodes_updated' => $context['results']['nodes_updated'],
+    '@nodes_processed' => $context['results']['nodes_processed'],
+    '@total' => $context['results']['nodes_count'],
+  ]);
+  $context['message'] = t('Updated @revisions_updated out of @revisions_processed revisions.', [
+    '@revisions_updated' => $context['results']['revisions_updated'],
+    '@revisions_processed' => $context['results']['revisions_processed'],
+  ]);
+}
+
+/**
+ * Final callback for the batch process updating block titles.
+ *
+ * @param bool $success
+ *   Indicates whether the batch process completed successfully.
+ * @param array $results
+ *   An array of results collected during the batch processing.
+ * @param array $operations
+ *   The remaining operations that were not processed, if any.
+ *
+ * @return string
+ *   Translated message indicating the completion of the batch process.
+ */
+function _references_block_titles_updates_batch_finished($success, $results, $operations) {
+  return t('Finished.');
 }
-- 
GitLab