From 20a06d0bd77ee6a176e92ecc2977d3aa3894a7ea Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Thu, 16 Jan 2025 14:41:05 +0530
Subject: [PATCH 01/13] Issue #3499722: Updated Render function.

---
 .../optional/views.view.block_content.yml     | 10 -----
 .../src/Plugin/views/area/ListingEmpty.php    | 45 +++++++++++--------
 2 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/core/modules/block_content/config/optional/views.view.block_content.yml b/core/modules/block_content/config/optional/views.view.block_content.yml
index 1bccbb446467..a2d7786fb453 100644
--- a/core/modules/block_content/config/optional/views.view.block_content.yml
+++ b/core/modules/block_content/config/optional/views.view.block_content.yml
@@ -304,16 +304,6 @@ display:
         options: {  }
       empty:
         area_text_custom:
-          id: area_text_custom
-          table: views
-          field: area_text_custom
-          relationship: none
-          group_type: group
-          admin_label: ''
-          plugin_id: text_custom
-          empty: true
-          content: 'There are no content blocks available.'
-          tokenize: false
         block_content_listing_empty:
           id: block_content_listing_empty
           table: block_content
diff --git a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
index b4ee4737e19b..e7d01a4c1a4c 100644
--- a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
+++ b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
@@ -65,25 +65,32 @@ public static function create(ContainerInterface $container, array $configuratio
     );
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function render($empty = FALSE) {
-    if (!$empty || !empty($this->options['empty'])) {
-      /** @var \Drupal\Core\Access\AccessResultInterface|\Drupal\Core\Cache\CacheableDependencyInterface $access_result */
-      $access_result = $this->accessManager->checkNamedRoute('block_content.add_page', [], $this->currentUser, TRUE);
-      $element = [
-        '#markup' => $this->t('Add a <a href=":url">content block</a>.', [':url' => Url::fromRoute('block_content.add_page')->toString()]),
-        '#access' => $access_result->isAllowed(),
-        '#cache' => [
-          'contexts' => $access_result->getCacheContexts(),
-          'tags' => $access_result->getCacheTags(),
-          'max-age' => $access_result->getCacheMaxAge(),
-        ],
-      ];
-      return $element;
-    }
-    return [];
+/**
+ * {@inheritdoc}
+ */
+public function render($empty = FALSE) {
+  if (!$empty || !empty($this->options['empty'])) {
+    // Construct the first sentence.
+    $message = $this->t('There are no content blocks available.');
+
+    // Construct the "Add a content block" link.
+    $add_link = $this->t('Add a <a href=":url">content block</a>.', [
+      ':url' => Url::fromRoute('block_content.add_page')->toString(),
+    ]);
+
+    // Combine both sentences with a space in between.
+    $element = [
+      '#markup' => $message . ' ' . $add_link,
+      '#cache' => [
+        'contexts' => ['user.permissions'],
+        // Cache for users with different access levels.
+      ],
+    ];
+
+    return $element;
   }
 
+  return [];
+}
+
 }
-- 
GitLab


From 80903df3654b712cad9dc729cf50f653c94b96c1 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Thu, 16 Jan 2025 15:11:24 +0530
Subject: [PATCH 02/13] Issue #3499722: Fixed phpcs and made content key empty.

---
 .../optional/views.view.block_content.yml     | 10 +++++
 .../src/Plugin/views/area/ListingEmpty.php    | 43 +++++++++----------
 2 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/core/modules/block_content/config/optional/views.view.block_content.yml b/core/modules/block_content/config/optional/views.view.block_content.yml
index a2d7786fb453..e440241bc190 100644
--- a/core/modules/block_content/config/optional/views.view.block_content.yml
+++ b/core/modules/block_content/config/optional/views.view.block_content.yml
@@ -304,6 +304,16 @@ display:
         options: {  }
       empty:
         area_text_custom:
+          id: area_text_custom
+          table: views
+          field: area_text_custom
+          relationship: none
+          group_type: group
+          admin_label: ''
+          plugin_id: text_custom
+          empty: true
+          content: ''
+          tokenize: false
         block_content_listing_empty:
           id: block_content_listing_empty
           table: block_content
diff --git a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
index e7d01a4c1a4c..a49883c8b922 100644
--- a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
+++ b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
@@ -65,32 +65,29 @@ public static function create(ContainerInterface $container, array $configuratio
     );
   }
 
-/**
- * {@inheritdoc}
- */
-public function render($empty = FALSE) {
-  if (!$empty || !empty($this->options['empty'])) {
-    // Construct the first sentence.
-    $message = $this->t('There are no content blocks available.');
+  /**
+   * {@inheritdoc}
+   */
+  public function render($empty = FALSE) {
+    if (!$empty || !empty($this->options['empty'])) {
+      $message = $this->t('There are no content blocks available.');
 
-    // Construct the "Add a content block" link.
-    $add_link = $this->t('Add a <a href=":url">content block</a>.', [
-      ':url' => Url::fromRoute('block_content.add_page')->toString(),
-    ]);
+      // Construct the "Add a content block" link.
+      $add_link = $this->t('Add a <a href=":url">content block</a>.', [
+        ':url' => Url::fromRoute('block_content.add_page')->toString(),
+      ]);
 
-    // Combine both sentences with a space in between.
-    $element = [
-      '#markup' => $message . ' ' . $add_link,
-      '#cache' => [
-        'contexts' => ['user.permissions'],
-        // Cache for users with different access levels.
-      ],
-    ];
+      $element = [
+        '#markup' => $message . ' ' . $add_link,
+        '#cache' => [
+          'contexts' => ['user.permissions'],
+        ],
+      ];
 
-    return $element;
-  }
+      return $element;
+    }
 
-  return [];
-}
+    return [];
+  }
 
 }
-- 
GitLab


From 37a853ab52df8eef43d29a4a258d24cb7346b4a5 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Thu, 16 Jan 2025 17:30:10 +0530
Subject: [PATCH 03/13] Issue #3499722: Updated caching.

---
 .../block_content/src/Plugin/views/area/ListingEmpty.php     | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
index a49883c8b922..7eda279f8ccb 100644
--- a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
+++ b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
@@ -76,11 +76,14 @@ public function render($empty = FALSE) {
       $add_link = $this->t('Add a <a href=":url">content block</a>.', [
         ':url' => Url::fromRoute('block_content.add_page')->toString(),
       ]);
+      $access_result = $this->accessManager->checkNamedRoute('block_content.add_page', [], $this->currentUser, TRUE);
 
       $element = [
         '#markup' => $message . ' ' . $add_link,
         '#cache' => [
-          'contexts' => ['user.permissions'],
+          'contexts' => $access_result->getCacheContexts(),
+          'tags' => $access_result->getCacheTags(),
+          'max-age' => $access_result->getCacheMaxAge(),
         ],
       ];
 
-- 
GitLab


From 0aca558898bb4c8a6a294c536a8d95b0396bb515 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Fri, 24 Jan 2025 11:21:02 +0530
Subject: [PATCH 04/13] Issue #3499722: Added update function,access check and
 removed pluggin.

---
 .../block_content/block_content.install       | 25 +++++++++++++++++++
 .../optional/views.view.block_content.yml     | 11 --------
 .../src/Plugin/views/area/ListingEmpty.php    | 16 ++++++++----
 3 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install
index 07b0bbbb5c39..c4d805ce532a 100644
--- a/core/modules/block_content/block_content.install
+++ b/core/modules/block_content/block_content.install
@@ -11,3 +11,28 @@
 function block_content_update_last_removed(): int {
   return 10300;
 }
+
+/**
+ * Removes the "area_text_custom" plugin from block content view configuration.
+ */
+function block_content_update_10301() {
+  $view = \Drupal::entityTypeManager()->getStorage('view')->load('block_content');
+
+  if ($view) {
+    $changed = FALSE;
+
+    // Check and remove the "area_text_custom" plugin from the 'empty' section.
+    foreach ($view->get('display') as $display_id => $display) {
+      if (isset($display['display_options']['empty']['area_text_custom'])) {
+        unset($display['display_options']['empty']['area_text_custom']);
+        $view->set('display.' . $display_id . '.display_options.empty.area_text_custom', NULL);
+        $changed = TRUE;
+      }
+    }
+
+    if ($changed) {
+      $view->save();
+      \Drupal::logger('block_content')->notice('The obsolete "area_text_custom" plugin has been removed from the block_content view.');
+    }
+  }
+}
diff --git a/core/modules/block_content/config/optional/views.view.block_content.yml b/core/modules/block_content/config/optional/views.view.block_content.yml
index e440241bc190..f0f47f48b7a7 100644
--- a/core/modules/block_content/config/optional/views.view.block_content.yml
+++ b/core/modules/block_content/config/optional/views.view.block_content.yml
@@ -303,17 +303,6 @@ display:
         type: tag
         options: {  }
       empty:
-        area_text_custom:
-          id: area_text_custom
-          table: views
-          field: area_text_custom
-          relationship: none
-          group_type: group
-          admin_label: ''
-          plugin_id: text_custom
-          empty: true
-          content: ''
-          tokenize: false
         block_content_listing_empty:
           id: block_content_listing_empty
           table: block_content
diff --git a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
index 7eda279f8ccb..c65f9cbc0e00 100644
--- a/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
+++ b/core/modules/block_content/src/Plugin/views/area/ListingEmpty.php
@@ -72,14 +72,20 @@ public function render($empty = FALSE) {
     if (!$empty || !empty($this->options['empty'])) {
       $message = $this->t('There are no content blocks available.');
 
-      // Construct the "Add a content block" link.
-      $add_link = $this->t('Add a <a href=":url">content block</a>.', [
-        ':url' => Url::fromRoute('block_content.add_page')->toString(),
-      ]);
+      // Construct the "Add a content block" link
+      // only if the user has the proper access.
       $access_result = $this->accessManager->checkNamedRoute('block_content.add_page', [], $this->currentUser, TRUE);
+      if ($access_result->isAllowed()) {
+        // Only show the link if the user has permission.
+        $add_link = $this->t('Add a <a href=":url">content block</a>.', [
+          ':url' => Url::fromRoute('block_content.add_page')->toString(),
+        ]);
+        // Combine the message and the link.
+        $message .= ' ' . $add_link;
+      }
 
       $element = [
-        '#markup' => $message . ' ' . $add_link,
+        '#markup' => $message,
         '#cache' => [
           'contexts' => $access_result->getCacheContexts(),
           'tags' => $access_result->getCacheTags(),
-- 
GitLab


From 6fdfb33f29c0e2eff63a571e700be253ed236ecb Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Fri, 24 Jan 2025 11:31:32 +0530
Subject: [PATCH 05/13] Issue #3499722: Added return type.

---
 core/modules/block_content/block_content.install | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install
index c4d805ce532a..a2e5047275f9 100644
--- a/core/modules/block_content/block_content.install
+++ b/core/modules/block_content/block_content.install
@@ -15,7 +15,7 @@ function block_content_update_last_removed(): int {
 /**
  * Removes the "area_text_custom" plugin from block content view configuration.
  */
-function block_content_update_10301() {
+function block_content_update_10301(): int {
   $view = \Drupal::entityTypeManager()->getStorage('view')->load('block_content');
 
   if ($view) {
@@ -35,4 +35,5 @@ function block_content_update_10301() {
       \Drupal::logger('block_content')->notice('The obsolete "area_text_custom" plugin has been removed from the block_content view.');
     }
   }
+  return \Drupal\Core\Update\UpdateHook::RESULT_OK;
 }
-- 
GitLab


From 87232e87394ef3345283df6134e697ceb28db1ee Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Fri, 24 Jan 2025 11:36:31 +0530
Subject: [PATCH 06/13] Issue #3499722: Fixed phpcs.

---
 core/modules/block_content/block_content.install | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install
index a2e5047275f9..c47f12103770 100644
--- a/core/modules/block_content/block_content.install
+++ b/core/modules/block_content/block_content.install
@@ -5,6 +5,8 @@
  * Install, update and uninstall functions for the block_content module.
  */
 
+use Drupal\Core\Update\UpdateHook;
+
 /**
  * Implements hook_update_last_removed().
  */
@@ -35,5 +37,5 @@ function block_content_update_10301(): int {
       \Drupal::logger('block_content')->notice('The obsolete "area_text_custom" plugin has been removed from the block_content view.');
     }
   }
-  return \Drupal\Core\Update\UpdateHook::RESULT_OK;
+  return UpdateHook::RESULT_OK;
 }
-- 
GitLab


From 1bcaab9ccf3a1dab01c93cd657d81554919ad106 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Fri, 24 Jan 2025 11:49:35 +0530
Subject: [PATCH 07/13] Issue #3499722: Removed updatehook.

---
 core/modules/block_content/block_content.install | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install
index c47f12103770..a5263a234b18 100644
--- a/core/modules/block_content/block_content.install
+++ b/core/modules/block_content/block_content.install
@@ -5,8 +5,6 @@
  * Install, update and uninstall functions for the block_content module.
  */
 
-use Drupal\Core\Update\UpdateHook;
-
 /**
  * Implements hook_update_last_removed().
  */
@@ -37,5 +35,5 @@ function block_content_update_10301(): int {
       \Drupal::logger('block_content')->notice('The obsolete "area_text_custom" plugin has been removed from the block_content view.');
     }
   }
-  return UpdateHook::RESULT_OK;
+  return 0;
 }
-- 
GitLab


From daf7e9e0582f5dd7570dd72dead7e006acc45095 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Tue, 4 Feb 2025 16:07:38 +0530
Subject: [PATCH 08/13] Issue #3499722: Used post_update and batch processing.

---
 .../block_content/block_content.install       | 26 -----------
 .../block_content.post_update.php             | 45 +++++++++++++++++++
 2 files changed, 45 insertions(+), 26 deletions(-)

diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install
index a5263a234b18..07b0bbbb5c39 100644
--- a/core/modules/block_content/block_content.install
+++ b/core/modules/block_content/block_content.install
@@ -11,29 +11,3 @@
 function block_content_update_last_removed(): int {
   return 10300;
 }
-
-/**
- * Removes the "area_text_custom" plugin from block content view configuration.
- */
-function block_content_update_10301(): int {
-  $view = \Drupal::entityTypeManager()->getStorage('view')->load('block_content');
-
-  if ($view) {
-    $changed = FALSE;
-
-    // Check and remove the "area_text_custom" plugin from the 'empty' section.
-    foreach ($view->get('display') as $display_id => $display) {
-      if (isset($display['display_options']['empty']['area_text_custom'])) {
-        unset($display['display_options']['empty']['area_text_custom']);
-        $view->set('display.' . $display_id . '.display_options.empty.area_text_custom', NULL);
-        $changed = TRUE;
-      }
-    }
-
-    if ($changed) {
-      $view->save();
-      \Drupal::logger('block_content')->notice('The obsolete "area_text_custom" plugin has been removed from the block_content view.');
-    }
-  }
-  return 0;
-}
diff --git a/core/modules/block_content/block_content.post_update.php b/core/modules/block_content/block_content.post_update.php
index 592b3ee97147..02b2af548c57 100644
--- a/core/modules/block_content/block_content.post_update.php
+++ b/core/modules/block_content/block_content.post_update.php
@@ -18,3 +18,48 @@ function block_content_removed_post_updates(): array {
     'block_content_post_update_revision_type' => '11.0.0',
   ];
 }
+
+/**
+ * Removes the "area_text_custom" plugin from block content view configuration.
+ */
+function block_content_post_update_10301(?array &$sandbox = NULL): int {
+  $storage = \Drupal::entityTypeManager()->getStorage('view');
+  $view_ids = $storage->getQuery()->execute();
+
+  // Initialize batch processing if first run.
+  if (!isset($sandbox['total'])) {
+    $sandbox['total'] = count($view_ids);
+    $sandbox['current_index'] = 0;
+  }
+
+  // Process views in chunks of 10.
+  $view_ids = array_slice($view_ids, $sandbox['current_index'], 10);
+  foreach ($view_ids as $view_id) {
+    $view = $storage->load($view_id);
+    if ($view) {
+      $changed = FALSE;
+      foreach ($view->get('display') as $display_id => $display) {
+        if (
+          isset($display['display_options']['empty']['area_text_custom']) &&
+          $display['display_options']['empty']['area_text_custom']['content'] === 'There are no content blocks available.'
+        ) {
+          unset($display['display_options']['empty']['area_text_custom']);
+          $view->set('display.' . $display_id . '.display_options.empty.area_text_custom', NULL);
+          $changed = TRUE;
+        }
+      }
+      if ($changed) {
+        $view->save();
+      }
+    }
+  }
+
+  // Update progress.
+  $sandbox['current_index'] += 10;
+  if ($sandbox['current_index'] < $sandbox['total']) {
+    return t('Updating block content views (@current/@total)', [
+      '@current' => $sandbox['current_index'],
+      '@total' => $sandbox['total'],
+    ]);
+  }
+}
-- 
GitLab


From c222a4ca0e985b689a24087db3a71b91a5c464f1 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Tue, 4 Feb 2025 18:12:45 +0530
Subject: [PATCH 09/13] Issue #3499722: Added test and return type.

---
 .../block_content.post_update.php             |  1 +
 .../src/Kernel/BlockContentUpdateTest.php     | 95 +++++++++++++++++++
 2 files changed, 96 insertions(+)
 create mode 100644 core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php

diff --git a/core/modules/block_content/block_content.post_update.php b/core/modules/block_content/block_content.post_update.php
index 02b2af548c57..24623cda1701 100644
--- a/core/modules/block_content/block_content.post_update.php
+++ b/core/modules/block_content/block_content.post_update.php
@@ -62,4 +62,5 @@ function block_content_post_update_10301(?array &$sandbox = NULL): int {
       '@total' => $sandbox['total'],
     ]);
   }
+  return 0;
 }
diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
new file mode 100644
index 000000000000..f95c1ef58b32
--- /dev/null
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace Drupal\Tests\block_content\Kernel;
+
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\views\Entity\View;
+
+/**
+ * Tests the block content update hook.
+ *
+ * @group block_content
+ */
+class BlockContentUpdateTest extends KernelTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  protected static $modules = [
+    'block_content',
+    'views',
+    'user',
+    'system',
+    'config',
+  ];
+
+  /**
+   * The test view.
+   *
+   * @var \Drupal\views\Entity\View
+   */
+  protected $view;
+
+  /**
+   * Setup test environment.
+   */
+  protected function setUp(): void {
+    parent::setUp();
+
+    $this->installEntitySchema('view');
+    $this->installConfig(['block_content', 'views']);
+
+    // Create a test view configuration with "area_text_custom".
+    $this->view = View::create([
+      'id' => 'block_content',
+      'label' => 'Block Content Test View',
+      'module' => 'views',
+      'status' => TRUE,
+      'display' => [
+        'default' => [
+          'display_options' => [
+            'empty' => [
+              'area_text_custom' => [
+                'id' => 'text_custom',
+                'content' => 'There are no content blocks available.',
+              ],
+            ],
+          ],
+        ],
+      ],
+    ]);
+    $this->view->save();
+  }
+
+  /**
+   * Tests the update function.
+   */
+  public function testUpdateHook(): void {
+    // Ensure the plugin exists before the update.
+    $this->assertTrue(isset($this->view->get('display')['default']['display_options']['empty']['area_text_custom']));
+
+    // Run the update function.
+    block_content_post_update_10301();
+
+    $updated_view = View::load('block_content');
+
+    // Verify the plugin is removed if it contains the default text.
+    $this->assertFalse(isset($updated_view->get('display')['default']['display_options']['empty']['area_text_custom']));
+  }
+
+  /**
+   * Tests that a customized text area is NOT removed.
+   */
+  public function testUpdateHookPreservesCustomText(): void {
+    $this->view->set('display.default.display_options.empty.area_text_custom.content', 'My custom message.');
+    $this->view->save();
+
+    block_content_post_update_10301();
+
+    $updated_view = View::load('block_content');
+
+    $this->assertTrue(isset($updated_view->get('display')['default']['display_options']['empty']['area_text_custom']));
+  }
+}
-- 
GitLab


From 8cfa37c0138803ce7df3c897ac29ea00301eebf6 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Tue, 4 Feb 2025 18:18:51 +0530
Subject: [PATCH 10/13] Issue #3499722: Fixed phpcs.

---
 .../block_content/tests/src/Kernel/BlockContentUpdateTest.php  | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
index f95c1ef58b32..4df843eb1269 100644
--- a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 namespace Drupal\Tests\block_content\Kernel;
 
 use Drupal\KernelTests\KernelTestBase;
@@ -92,4 +94,5 @@ public function testUpdateHookPreservesCustomText(): void {
 
     $this->assertTrue(isset($updated_view->get('display')['default']['display_options']['empty']['area_text_custom']));
   }
+
 }
-- 
GitLab


From c111bbf3c066ae727e85f25a73fbfcc7853eaa73 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Wed, 5 Feb 2025 11:35:46 +0530
Subject: [PATCH 11/13] Issue #3499722: Used ConfigEntityUpdater.

---
 .../block_content.post_update.php             | 48 +++++--------------
 1 file changed, 13 insertions(+), 35 deletions(-)

diff --git a/core/modules/block_content/block_content.post_update.php b/core/modules/block_content/block_content.post_update.php
index 24623cda1701..73dd5faba7e2 100644
--- a/core/modules/block_content/block_content.post_update.php
+++ b/core/modules/block_content/block_content.post_update.php
@@ -23,44 +23,22 @@ function block_content_removed_post_updates(): array {
  * Removes the "area_text_custom" plugin from block content view configuration.
  */
 function block_content_post_update_10301(?array &$sandbox = NULL): int {
-  $storage = \Drupal::entityTypeManager()->getStorage('view');
-  $view_ids = $storage->getQuery()->execute();
+  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function (ViewEntityInterface $view): bool {
+    $changed = FALSE;
 
-  // Initialize batch processing if first run.
-  if (!isset($sandbox['total'])) {
-    $sandbox['total'] = count($view_ids);
-    $sandbox['current_index'] = 0;
-  }
-
-  // Process views in chunks of 10.
-  $view_ids = array_slice($view_ids, $sandbox['current_index'], 10);
-  foreach ($view_ids as $view_id) {
-    $view = $storage->load($view_id);
-    if ($view) {
-      $changed = FALSE;
-      foreach ($view->get('display') as $display_id => $display) {
-        if (
-          isset($display['display_options']['empty']['area_text_custom']) &&
-          $display['display_options']['empty']['area_text_custom']['content'] === 'There are no content blocks available.'
-        ) {
-          unset($display['display_options']['empty']['area_text_custom']);
-          $view->set('display.' . $display_id . '.display_options.empty.area_text_custom', NULL);
-          $changed = TRUE;
-        }
-      }
-      if ($changed) {
-        $view->save();
+    foreach ($view->get('display') as $display_id => $display) {
+      if (
+        isset($display['display_options']['empty']['area_text_custom']) &&
+        $display['display_options']['empty']['area_text_custom']['content'] === 'There are no content blocks available.'
+      ) {
+        unset($display['display_options']['empty']['area_text_custom']);
+        $view->set('display.' . $display_id . '.display_options.empty.area_text_custom', NULL);
+        $changed = TRUE;
       }
     }
-  }
 
-  // Update progress.
-  $sandbox['current_index'] += 10;
-  if ($sandbox['current_index'] < $sandbox['total']) {
-    return t('Updating block content views (@current/@total)', [
-      '@current' => $sandbox['current_index'],
-      '@total' => $sandbox['total'],
-    ]);
-  }
+    return $changed;
+  });
+
   return 0;
 }
-- 
GitLab


From 978c5803c2dc043c9621bbe940ad429dd4940969 Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Wed, 5 Feb 2025 12:02:43 +0530
Subject: [PATCH 12/13] Issue #3499722: Added classes and updated test.

---
 .../block_content.post_update.php             |  3 ++
 .../src/Kernel/BlockContentUpdateTest.php     | 48 +++++++++++++++++--
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/core/modules/block_content/block_content.post_update.php b/core/modules/block_content/block_content.post_update.php
index 73dd5faba7e2..280d2ba59f42 100644
--- a/core/modules/block_content/block_content.post_update.php
+++ b/core/modules/block_content/block_content.post_update.php
@@ -5,6 +5,9 @@
  * Post update functions for Content Block.
  */
 
+use Drupal\Core\Config\Entity\ConfigEntityUpdater;
+use Drupal\views\ViewEntityInterface;
+
 /**
  * Implements hook_removed_post_updates().
  */
diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
index 4df843eb1269..d165dc872313 100644
--- a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
@@ -72,8 +72,9 @@ public function testUpdateHook(): void {
     // Ensure the plugin exists before the update.
     $this->assertTrue(isset($this->view->get('display')['default']['display_options']['empty']['area_text_custom']));
 
-    // Run the update function.
-    block_content_post_update_10301();
+    // Run the update function with sandbox.
+    $sandbox = [];
+    block_content_post_update_10301($sandbox);
 
     $updated_view = View::load('block_content');
 
@@ -88,11 +89,52 @@ public function testUpdateHookPreservesCustomText(): void {
     $this->view->set('display.default.display_options.empty.area_text_custom.content', 'My custom message.');
     $this->view->save();
 
-    block_content_post_update_10301();
+    $sandbox = [];
+    block_content_post_update_10301($sandbox);
 
     $updated_view = View::load('block_content');
 
     $this->assertTrue(isset($updated_view->get('display')['default']['display_options']['empty']['area_text_custom']));
   }
 
+  /**
+   * Tests batch processing of multiple views.
+   */
+  public function testBatchProcessing(): void {
+    // Create additional views for batch testing.
+    for ($i = 1; $i <= 15; $i++) {
+      $view = View::create([
+        'id' => "block_content_$i",
+        'label' => "Block Content Test View $i",
+        'module' => 'views',
+        'status' => TRUE,
+        'display' => [
+          'default' => [
+            'display_options' => [
+              'empty' => [
+                'area_text_custom' => [
+                  'id' => 'text_custom',
+                  'content' => 'There are no content blocks available.',
+                ],
+              ],
+            ],
+          ],
+        ],
+      ]);
+      $view->save();
+    }
+
+    // Run the update in batches.
+    $sandbox = [];
+    do {
+      block_content_post_update_10301($sandbox);
+    } while (!empty($sandbox));
+
+    // Verify that all views have been processed.
+    for ($i = 1; $i <= 15; $i++) {
+      $updated_view = View::load("block_content_$i");
+      $this->assertFalse(isset($updated_view->get('display')['default']['display_options']['empty']['area_text_custom']));
+    }
+  }
+
 }
-- 
GitLab


From 2753253a55f4455cf138742abb1ab505bca179ba Mon Sep 17 00:00:00 2001
From: arunsahijpal <arunsahijpal111@gmail.com>
Date: Thu, 6 Feb 2025 12:07:00 +0530
Subject: [PATCH 13/13] Issue #3499722: Updated test.

---
 .../block_content/tests/src/Kernel/BlockContentUpdateTest.php   | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
index d165dc872313..0c080706b93f 100644
--- a/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentUpdateTest.php
@@ -41,6 +41,8 @@ protected function setUp(): void {
     parent::setUp();
 
     $this->installEntitySchema('view');
+    $this->installEntitySchema('block_content');
+    $this->installEntitySchema('user');
     $this->installConfig(['block_content', 'views']);
 
     // Create a test view configuration with "area_text_custom".
-- 
GitLab