From 2d3c7f1642b58dc30530503ece0c957ee20af366 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Mon, 25 Aug 2014 23:06:23 -0700
Subject: [PATCH] Issue #2303521 by swentel, larowlan | alexpott: Fixed
 CommentTypeForm allows the entity type to be changed.

---
 core/modules/comment/src/CommentTypeForm.php  | 30 ++++++++++++-------
 .../comment/src/Tests/CommentTypeTest.php     | 11 +++++++
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/core/modules/comment/src/CommentTypeForm.php b/core/modules/comment/src/CommentTypeForm.php
index 418aea1a33c2..0d93b1c05004 100644
--- a/core/modules/comment/src/CommentTypeForm.php
+++ b/core/modules/comment/src/CommentTypeForm.php
@@ -88,18 +88,28 @@ public function form(array $form, FormStateInterface $form_state) {
       '#title' => t('Description'),
     );
 
-    $options = array();
-    foreach ($this->entityManager->getDefinitions() as $entity_type) {
-      if ($entity_type->isFieldable()) {
-        $options[$entity_type->id()] = $entity_type->getLabel();
+    if ($comment_type->isNew()) {
+      $options = array();
+      foreach ($this->entityManager->getDefinitions() as $entity_type) {
+        if ($entity_type->isFieldable()) {
+          $options[$entity_type->id()] = $entity_type->getLabel();
+        }
       }
+      $form['target_entity_type_id'] = array(
+        '#type' => 'select',
+        '#default_value' => $comment_type->getTargetEntityTypeId(),
+        '#title' => t('Target entity type'),
+        '#options' => $options,
+        '#description' => t('The target entity type can not be changed after the comment type has been created.')
+      );
+    }
+    else {
+      $form['target_entity_type_id_display'] = array(
+        '#type' => 'item',
+        '#markup' => $this->entityManager->getDefinition($comment_type->getTargetEntityTypeId())->getLabel(),
+        '#title' => t('Target entity type'),
+      );
     }
-    $form['target_entity_type_id'] = array(
-      '#type' => 'select',
-      '#default_value' => $comment_type->getTargetEntityTypeId(),
-      '#title' => t('Target entity type'),
-      '#options' => $options,
-    );
 
     if ($this->moduleHandler->moduleExists('content_translation')) {
       $form['language'] = array(
diff --git a/core/modules/comment/src/Tests/CommentTypeTest.php b/core/modules/comment/src/Tests/CommentTypeTest.php
index bf687ee89f42..87fb38bc40c5 100644
--- a/core/modules/comment/src/Tests/CommentTypeTest.php
+++ b/core/modules/comment/src/Tests/CommentTypeTest.php
@@ -76,6 +76,17 @@ public function testCommentTypeCreation() {
     // Check that the comment type was created in site default language.
     $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->id;
     $this->assertEqual($comment_type->language()->getId(), $default_langcode);
+
+    // Edit the comment-type and ensure that we cannot change the entity-type.
+    $this->drupalGet('admin/structure/comment/manage/foo');
+    $this->assertNoField('target_entity_type_id', 'Entity type file not present');
+    $this->assertText(t('Target entity type'));
+    // Save the form and ensure the entity-type value is preserved even though
+    // the field isn't present.
+    $this->drupalPostForm(NULL, array(), t('Save'));
+    \Drupal::entityManager()->getStorage('comment_type')->resetCache(array('foo'));
+    $comment_type = CommentType::load('foo');
+    $this->assertEqual($comment_type->getTargetEntityTypeId(), 'node');
   }
 
   /**
-- 
GitLab