From 3fb92dbefbdf7f0899d1bc43ff60929de9c03afe Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Tue, 13 Jul 2021 17:14:49 -0400
Subject: [PATCH] ISTWCMS-4939: adding update hook to set default num of items
 for listing blocks

---
 uw_cfg_common.install | 114 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)

diff --git a/uw_cfg_common.install b/uw_cfg_common.install
index 004de253..596f35ca 100644
--- a/uw_cfg_common.install
+++ b/uw_cfg_common.install
@@ -6,6 +6,7 @@
  */
 
 use Drupal\taxonomy\Entity\Term;
+use \Drupal\node\Entity\Node;
 use Drupal\uw_cfg_common\UwPermissions\UwPermissions;
 
 /**
@@ -239,3 +240,116 @@ function _uw_cfg_common_create_term($taxonomy_name, $vocab_machine_name, $weight
   // Return the taxonomy term id.
   return $new_term->id();
 }
+
+/**
+ * Set default settings for listing page blocks.
+ */
+function uw_cfg_common_update_8101() {
+
+  // Block ids that need to be changed.
+  $block_ids = [
+    'views_block:uw_view_blogs-blogs_listing_block',
+    'views_block:uw_view_events-events_listing_block',
+    'views_block:uw_view_news_items-news_items_listing_block',
+  ];
+
+  // Load all the nodes.
+  $nodes = \Drupal\node\Entity\Node::loadMultiple();
+
+  // Step through each node and set the default setting
+  // to 3 on listing blocks.
+  foreach ($nodes as $node) {
+
+    // Flag to save the node, have this to save processing
+    // time if we don't need to save the node after the checks.
+    $save_node_flag = FALSE;
+
+    // Load the layout and sections.
+    $layout = $node->get('layout_builder__layout');
+    $sections = $layout->getSections();
+
+    // Step through each of the sections.
+    foreach ($sections as $section) {
+
+      // Load the components for the section.
+      $components = $section->getComponents();
+
+      // Step through each of the components.
+      foreach ($components as $component) {
+
+        // If this component is one that needs to be changed,
+        // then check for setting and change if required.
+        if (in_array($component->getPluginId(), $block_ids)) {
+
+          // Load the config for the block.
+          $configurations = $component->get('configuration');
+
+          // If the config is none of 5, it needs to be changed to 3.
+          if (
+            $configurations['items_per_page'] == 'none' ||
+            $configurations['items_per_page'] == '5'
+          ) {
+
+            // Change the config and save the component.
+            $configurations['items_per_page'] = '3';
+            $component->setConfiguration($configurations);
+
+            // Set the save node flag so that we know to
+            // save this node as the last step in the loop.
+            $save_node_flag = TRUE;
+          }
+        }
+      }
+
+      // If we need to save the node, then save it.
+      if ($save_node_flag) {
+        $node->save();
+      }
+    }
+
+    // Load all the revisions for the node.
+    $vids = \Drupal::service('entity_type.manager')->getStorage('node')->revisionIds($node);
+
+    // Step through each revision, and check if we have to
+    // change the settings for the listing blocks.
+    foreach ($vids as $vid) {
+
+      // Flag to see if we have to save the revision.
+      $save_revision_flag = FALSE;
+
+      // Load the revision node.
+      $revision_node = \Drupal::entityManager()->getStorage('node')->loadRevision($vid);
+
+      // Comments from here down are the same as above.
+      $layout = $revision_node->get('layout_builder__layout');
+      $sections = $layout->getSections();
+
+      foreach ($sections as $section) {
+
+        $components = $section->getComponents();
+
+        foreach ($components as $component) {
+
+          if (in_array($component->getPluginId(), $block_ids)) {
+
+            $configurations = $component->get('configuration');
+
+            if (
+              $configurations['items_per_page'] == 'none' ||
+              $configurations['items_per_page'] == '5'
+            ) {
+
+              $configurations['items_per_page'] = '3';
+              $component->setConfiguration($configurations);
+              $save_revision_flag = TRUE;
+            }
+          }
+        }
+      }
+
+      if ($save_revision_flag) {
+        $revision_node->save();
+      }
+    }
+  }
+}
-- 
GitLab