From f680053507abffe4485016faba3c7c11f53ada7f Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Fri, 14 Jul 2023 11:19:03 +0100
Subject: [PATCH] Issue #3373938 by lauriii, keshav.k: Comment settings are
 confusing

---
 .../Plugin/Field/FieldWidget/CommentWidget.php  | 17 +++++++----------
 .../tests/src/Functional/CommentNonNodeTest.php |  5 +++--
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php b/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php
index e7b3898dfb04..6e466e05b8b1 100644
--- a/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php
+++ b/core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php
@@ -32,6 +32,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
       '#title' => $this->t('Comments'),
       '#title_display' => 'invisible',
       '#default_value' => $items->status,
+      '#required ' => TRUE,
       '#options' => [
         CommentItemInterface::OPEN => $this->t('Open'),
         CommentItemInterface::CLOSED => $this->t('Closed'),
@@ -44,22 +45,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
         '#description' => $this->t('Users cannot post comments, but existing comments will be displayed.'),
       ],
       CommentItemInterface::HIDDEN => [
-        '#description' => $this->t('Comments are hidden from view.'),
+        '#description' => $this->t('Comments and the comment form are hidden from view.'),
       ],
     ];
 
-    // Setting a value in the default value widget is required.
-    if ($this->isDefaultValueWidget($form_state)) {
-      $element['status']['#required'] = TRUE;
-    }
-
-    // If the entity doesn't have any comments, the "hidden" option makes no
+    // If the entity doesn't have any comments, the "Closed" option makes no
     // sense, so don't even bother presenting it to the user unless this is the
     // default value widget on the field settings form.
     if (!$this->isDefaultValueWidget($form_state) && !$items->comment_count) {
-      $element['status'][CommentItemInterface::HIDDEN]['#access'] = FALSE;
-      // Also adjust the description of the "closed" option.
-      $element['status'][CommentItemInterface::CLOSED]['#description'] = $this->t('Users cannot post comments.');
+      // Only hide the option when it's not the currently selected option.
+      if ($element['status']['#default_value'] !== CommentItemInterface::CLOSED) {
+        $element['status'][CommentItemInterface::CLOSED]['#access'] = FALSE;
+      }
     }
     // If the advanced settings tabs-set is available (normally rendered in the
     // second column on wide-resolutions), place the field as a details element
diff --git a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php
index 07e10871ea58..c66865da1017 100644
--- a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php
+++ b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php
@@ -458,9 +458,9 @@ public function testCommentFunctionality() {
     $new_entity = EntityTest::create($data);
     $new_entity->save();
     $this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit');
-    $this->assertSession()->checkboxNotChecked('edit-field-foobar-0-status-1');
     $this->assertSession()->checkboxChecked('edit-field-foobar-0-status-2');
-    $this->assertSession()->fieldNotExists('edit-field-foobar-0-status-0');
+    $this->assertSession()->checkboxNotChecked('edit-field-foobar-0-status-0');
+    $this->assertSession()->fieldNotExists('edit-field-foobar-0-status-1');
 
     // @todo Check proper URL and form https://www.drupal.org/node/2458323
     $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
@@ -544,6 +544,7 @@ public function testCommentSettingsNotRequired() {
     // with the default value.
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('/entity_test/add');
+    $this->assertSession()->checkboxChecked('edit-comment-0-status-0');
     $edit = [
       "name[0][value]" => 'Comment test',
     ];
-- 
GitLab