Verified Commit 023898d3 authored by Dave Long's avatar Dave Long
Browse files

Issue #2587415 by lauriii, LoMo, hooroomoo, longwave: Comment types UI confusing and inconsistent

parent c61d707e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -118,6 +118,8 @@ public function form(array $form, FormStateInterface $form_state) {
        '#type' => 'select',
        '#default_value' => $comment_type->getTargetEntityTypeId(),
        '#title' => $this->t('Target entity type'),
        '#required' => TRUE,
        '#empty_value' => '_none',
        '#options' => $options,
        '#description' => $this->t('The target entity type can not be changed after the comment type has been created.'),
      ];
+33 −0
Original line number Diff line number Diff line
@@ -4,6 +4,10 @@

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a class to build a listing of comment type entities.
@@ -12,6 +16,31 @@
 */
class CommentTypeListBuilder extends ConfigEntityListBuilder {

  /**
   * Constructs a new CommentTypeListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, protected EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($entity_type, $storage);
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('entity_type.manager')->getStorage($entity_type->id()),
      $container->get('entity_type.manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -31,6 +60,7 @@ public function getDefaultOperations(EntityInterface $entity) {
  public function buildHeader() {
    $header['type'] = t('Comment type');
    $header['description'] = t('Description');
    $header['target'] = t('Target entity type');
    return $header + parent::buildHeader();
  }

@@ -38,8 +68,11 @@ public function buildHeader() {
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    assert($entity instanceof CommentTypeInterface);
    $entity_type = $this->entityTypeManager->getDefinition($entity->getTargetEntityTypeId());
    $row['type'] = $entity->label();
    $row['description']['data'] = ['#markup' => $entity->getDescription()];
    $row['target'] = $entity_type->getLabel();
    return $row + parent::buildRow($entity);
  }

+12 −1
Original line number Diff line number Diff line
@@ -75,10 +75,17 @@ public function testCommentTypeCreation() {
      'id' => 'foo',
      'label' => 'title for foo',
      'description' => '',
      'target_entity_type_id' => 'node',
    ];
    $this->drupalGet('admin/structure/comment/types/add');

    // Ensure that target entity type is a required field.
    $this->submitForm($edit, 'Save and manage fields');
    $this->assertSession()->pageTextContains('Target entity type field is required.');

    // Ensure that comment type is saved when target entity type is provided.
    $edit['target_entity_type_id'] = 'node';
    $this->submitForm($edit, 'Save and manage fields');
    $this->assertSession()->pageTextContains('Comment type title for foo has been added.');

    // Asserts that form submit redirects to the expected manage fields page.
    $this->assertSession()->addressEquals('admin/structure/comment/manage/' . $edit['id'] . '/fields');
@@ -103,6 +110,10 @@ public function testCommentTypeCreation() {
    \Drupal::entityTypeManager()->getStorage('comment_type')->resetCache(['foo']);
    $comment_type = CommentType::load('foo');
    $this->assertEquals('node', $comment_type->getTargetEntityTypeId());

    // Ensure that target type is displayed in the comment type list.
    $this->drupalGet('admin/structure/comment');
    $this->assertSession()->elementExists('xpath', '//td[text() = "Content"]');
  }

  /**