From b3af2575fa97e4ae78c054046b8069ba5962645e Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Sun, 31 Jul 2022 20:17:27 +0900
Subject: [PATCH] Issue #2774399 by mohit_aghera, joebot, Munavijayalakshmi,
 rensingh99, borisson_, Chi, quietone: Fatal error on accessing module
 uninstall page with Forum enabled

---
 .../forum/src/ForumUninstallValidator.php     |  9 +++++++--
 .../src/Functional/ForumUninstallTest.php     | 20 +++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/core/modules/forum/src/ForumUninstallValidator.php b/core/modules/forum/src/ForumUninstallValidator.php
index face3cbf459c..a67caac31694 100644
--- a/core/modules/forum/src/ForumUninstallValidator.php
+++ b/core/modules/forum/src/ForumUninstallValidator.php
@@ -58,7 +58,7 @@ public function validate($module) {
       }
 
       $vocabulary = $this->getForumVocabulary();
-      if ($this->hasTermsForVocabulary($vocabulary)) {
+      if (!empty($vocabulary) && $this->hasTermsForVocabulary($vocabulary)) {
         if ($vocabulary->access('view')) {
           $reasons[] = $this->t('To uninstall Forum, first delete all <a href=":url">%vocabulary</a> terms', [
             '%vocabulary' => $vocabulary->label(),
@@ -117,7 +117,12 @@ protected function hasTermsForVocabulary(VocabularyInterface $vocabulary) {
    */
   protected function getForumVocabulary() {
     $vid = $this->configFactory->get('forum.settings')->get('vocabulary');
-    return $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid);
+    if (!empty($vid)) {
+      return $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid);
+    }
+    else {
+      return NULL;
+    }
   }
 
 }
diff --git a/core/modules/forum/tests/src/Functional/ForumUninstallTest.php b/core/modules/forum/tests/src/Functional/ForumUninstallTest.php
index 30ea8c1fd07a..1e2c6fa66e90 100644
--- a/core/modules/forum/tests/src/Functional/ForumUninstallTest.php
+++ b/core/modules/forum/tests/src/Functional/ForumUninstallTest.php
@@ -7,6 +7,7 @@
 use Drupal\node\Entity\NodeType;
 use Drupal\comment\Entity\Comment;
 use Drupal\taxonomy\Entity\Term;
+use Drupal\taxonomy\Entity\Vocabulary;
 use Drupal\Tests\BrowserTestBase;
 
 /**
@@ -159,4 +160,23 @@ public function testForumUninstallWithoutFieldStorage() {
     $this->container->get('module_installer')->uninstall(['forum']);
   }
 
+  /**
+   * Tests uninstallation of forum module when vocabulary is deleted.
+   */
+  public function testForumUninstallWithoutForumVocabulary() {
+    $this->drupalLogin($this->rootUser);
+    Vocabulary::load('forums')->delete();
+
+    // Now attempt to uninstall forum.
+    $this->drupalGet('admin/modules/uninstall');
+    $this->assertSession()->responseNotContains('The website encountered an unexpected error. Please try again later');
+    $this->assertSession()->statusCodeEquals(200);
+
+    // Assert forum is no longer required.
+    $this->assertSession()->fieldExists('uninstall[forum]');
+
+    $this->drupalGet('admin/modules/uninstall');
+    $this->submitForm(['uninstall[forum]' => 1], 'Uninstall');
+  }
+
 }
-- 
GitLab