diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 9d0a3d8..f5601f0 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -53,7 +53,7 @@ function comment_schema() { 'type' => 'varchar', 'not null' => TRUE, 'default' => '', - 'length' => 255, + 'length' => 32, 'description' => 'The field_name of the field that was used to add this comment.', ), 'cid' => array( @@ -89,7 +89,7 @@ function comment_schema() { 'description' => 'The total number of comments on this entity.', ), ), - 'primary key' => array('entity_id', 'entity_type', array('field_name', 32)), + 'primary key' => array('entity_id', 'entity_type', 'field_name'), 'indexes' => array( 'last_comment_timestamp' => array('last_comment_timestamp'), 'comment_count' => array('comment_count'), diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php index f840d35..d1d49a3 100644 --- a/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -18,6 +18,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldInstanceConfig; /** * Comment manager contains common functions to manage comment fields. @@ -211,9 +212,7 @@ public function addBodyField($comment_type_id) { )); $field->save(); } - $field_instances = $this->entityManager - ->getFieldDefinitions('comment', $comment_type_id); - if (empty($field_instances['comment_body'])) { + if (!FieldInstanceConfig::loadByName('comment', $comment_type_id, 'comment_body')) { // Attaches the body field by default. $field_instance = $this->entityManager->getStorage('field_instance_config')->create(array( 'field_name' => 'comment_body', diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index 1f808eb..2cffb5e 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -148,7 +148,7 @@ public function getSchema() { 'comment__num_new' => array( 'entity_id', array('entity_type', 32), - array('comment_type', EntityTypeInterface::BUNDLE_MAX_LENGTH), + 'comment_type', 'status', 'created', 'cid', @@ -157,7 +157,7 @@ public function getSchema() { 'comment__entity_langcode' => array( 'entity_id', array('entity_type', 32), - array('comment_type', EntityTypeInterface::BUNDLE_MAX_LENGTH), + 'comment_type', 'langcode', ), 'comment__created' => array('created'), diff --git a/core/modules/comment/src/CommentTypeInterface.php b/core/modules/comment/src/CommentTypeInterface.php index 215fe12..3f6fc73 100644 --- a/core/modules/comment/src/CommentTypeInterface.php +++ b/core/modules/comment/src/CommentTypeInterface.php @@ -15,6 +15,11 @@ interface CommentTypeInterface extends ConfigEntityInterface { /** + * Keyed used to indicate a new comment type. + */ + CONST NEW_PLACEHOLDER = ':new'; + + /** * Returns the comment type description. * * @return string diff --git a/core/modules/comment/src/CommentTypeListBuilder.php b/core/modules/comment/src/CommentTypeListBuilder.php index faadcdc..fd478f6 100644 --- a/core/modules/comment/src/CommentTypeListBuilder.php +++ b/core/modules/comment/src/CommentTypeListBuilder.php @@ -7,6 +7,7 @@ namespace Drupal\comment; +use Drupal\Component\Utility\String; use Drupal\Component\Utility\Xss; use Drupal\Core\Config\Entity\ConfigEntityListBuilder; use Drupal\Core\Entity\EntityInterface; @@ -23,41 +24,6 @@ class CommentTypeListBuilder extends ConfigEntityListBuilder { /** - * The link generator service. - * - * @var \Drupal\Core\Utility\LinkGeneratorInterface - */ - - protected $linkGenerator; - /** - * {@inheritdoc} - */ - public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { - return new static( - $entity_type, - $container->get('entity.manager')->getStorage($entity_type->id()), - $container->get('link_generator') - ); - } - - /** - * Constructs a new EntityListBuilder 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\Utility\LinkGeneratorInterface $link_generator - * The link generator service. - */ - public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, LinkGeneratorInterface $link_generator) { - $this->entityTypeId = $entity_type->id(); - $this->storage = $storage; - $this->entityType = $entity_type; - $this->linkGenerator = $link_generator; - } - - /** * {@inheritdoc} */ public function getDefaultOperations(EntityInterface $entity) { @@ -83,7 +49,7 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - $row['type'] = $this->linkGenerator->generateFromUrl($entity->label(), $entity->urlInfo()); + $row['type'] = String::checkPlain($entity->label()); $row['description'] = Xss::filterAdmin($entity->getDescription()); return $row + parent::buildRow($entity); } diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 0c42ca0..6beb991 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -285,15 +285,15 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Entity type')) ->setDescription(t('The entity type to which this comment is attached.')); - $fields['comment_type'] = FieldDefinition::create('string') + $fields['comment_type'] = FieldDefinition::create('entity_reference') ->setLabel(t('Comment Type')) ->setDescription(t('The comment type.')) - ->setSetting('max_length', EntityTypeInterface::BUNDLE_MAX_LENGTH); + ->setSetting('target_type', 'comment_type'); $fields['field_name'] = FieldDefinition::create('string') ->setLabel(t('Comment field name')) ->setDescription(t('The field name through which this comment was added.')) - ->setSetting('max_length', EntityTypeInterface::BUNDLE_MAX_LENGTH); + ->setSetting('max_length', 32); return $fields; } @@ -533,10 +533,10 @@ public function setOwner(UserInterface $account) { } /** - * Get the comment type id for this comment. + * Get the comment type ID for this comment. * * @return string - * The id of the comment type. + * The ID of the comment type. */ public function getTypeId() { return $this->bundle(); diff --git a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php index fcea82c..f92cc6d 100644 --- a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Plugin\Field\FieldType; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Entity\CommentType; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; @@ -199,7 +200,7 @@ public function settingsForm(array &$form, array &$form_state, $has_data) { // @todo Inject entity storage once typed-data supports container injection. $comment_types = CommentType::loadMultiple(); - $options = array('' => t('Create new')); + $options = array(CommentTypeInterface::NEW_PLACEHOLDER => t('Create new')); $entity_type = $this->getEntity()->getEntityTypeId(); foreach ($comment_types as $comment_type) { if ($comment_type->getTargetEntityTypeId() == $entity_type) { @@ -264,9 +265,9 @@ public function settingsForm(array &$form, array &$form_state, $has_data) { */ public function createNewCommentType($form, &$form_state) { $settings = $form_state['values']['field']['settings']; - if ($settings['comment_type'] === '') { + if ($settings['comment_type'] === CommentTypeInterface::NEW_PLACEHOLDER) { // @todo Inject this once typed-data supports ContainerInjectionInterface. - $comment_type = entity_create('comment_type', $settings['new_comment_type']); + $comment_type = CommentType::create($settings['new_comment_type']); $comment_type->save(); $form_state['values']['field']['settings']['comment_type'] = $comment_type->id(); } @@ -277,7 +278,7 @@ public function createNewCommentType($form, &$form_state) { */ public function validateNewCommentType($form, &$form_state) { $settings = $form_state['values']['field']['settings']; - if ($settings['comment_type'] == '') { + if ($settings['comment_type'] == CommentTypeInterface::NEW_PLACEHOLDER) { if (empty($settings['new_comment_type']['label'])) { \Drupal::formBuilder()->setErrorByName('field][settings][new_comment_type][label', $form_state, t('Please provide a label for the new comment type')); } diff --git a/core/modules/comment/src/Tests/CommentNonNodeTest.php b/core/modules/comment/src/Tests/CommentNonNodeTest.php index bd19af6..6f2d3da 100644 --- a/core/modules/comment/src/Tests/CommentNonNodeTest.php +++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php @@ -8,6 +8,7 @@ namespace Drupal\comment\Tests; use Drupal\comment\CommentInterface; +use Drupal\comment\CommentTypeInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\field\Entity\FieldInstanceConfig; use Drupal\simpletest\WebTestBase; @@ -371,13 +372,13 @@ function testCommentFunctionality() { $this->drupalPostForm(NULL, $edit, t('Save')); // Attempt to save with a new comment-type but incomplete form. $this->drupalPostForm(NULL, array( - 'field[settings][comment_type]' => '', + 'field[settings][comment_type]' => CommentTypeInterface::NEW_PLACEHOLDER, ), t('Save field settings')); $this->assertText(t('Please provide a label for the new comment type')); $this->assertText(t('Please provide a machine name for the new comment type')); // Now provide the required values. $this->drupalPostForm(NULL, array( - 'field[settings][comment_type]' => '', + 'field[settings][comment_type]' => CommentTypeInterface::NEW_PLACEHOLDER, 'field[settings][new_comment_type][id]' => 'foobar', 'field[settings][new_comment_type][label]' => 'Foobar', ), t('Save field settings')); diff --git a/core/modules/comment/src/Tests/CommentTestBase.php b/core/modules/comment/src/Tests/CommentTestBase.php index a64dd7d..37a9589 100644 --- a/core/modules/comment/src/Tests/CommentTestBase.php +++ b/core/modules/comment/src/Tests/CommentTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Tests; +use Drupal\comment\Entity\CommentType; use Drupal\Core\Language\Language; use Drupal\comment\CommentInterface; use Drupal\field\Entity\FieldInstanceConfig; @@ -355,4 +356,24 @@ function getUnapprovedComment($subject) { return $match[2]; } + /** + * Creates a comment comment type (bundle). + * + * @param string $label + * The comment type label. + * + * @return \Drupal\comment\Entity\CommentType + * Created comment type. + */ + protected function createCommentType($label) { + $bundle = CommentType::create(array( + 'id' => $label, + 'label' => $label, + 'description' => '', + 'target_entity_type_id' => 'node', + )); + $bundle->save(); + return $bundle; + } + } diff --git a/core/modules/comment/src/Tests/CommentTypeTest.php b/core/modules/comment/src/Tests/CommentTypeTest.php index 1153abc..edc9456 100644 --- a/core/modules/comment/src/Tests/CommentTypeTest.php +++ b/core/modules/comment/src/Tests/CommentTypeTest.php @@ -6,9 +6,12 @@ */ namespace Drupal\comment\Tests; +use Drupal\comment\Entity\Comment; +use Drupal\comment\Entity\CommentType; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldInstanceConfig; +use Drupal\node\Entity\Node; /** * Tests related to custom comment types. @@ -59,7 +62,7 @@ public function testCommentTypeCreation() { // Create a comment type programmaticaly. $type = $this->createCommentType('other'); - $comment_type = entity_load('comment_type', 'other'); + $comment_type = CommentType::load('other'); $this->assertTrue($comment_type, 'The new comment type has been created.'); // Login a test user. @@ -76,7 +79,7 @@ public function testCommentTypeCreation() { 'target_entity_type_id' => 'node', ); $this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save')); - $comment_type = entity_load('comment_type', 'foo'); + $comment_type = CommentType::load('foo'); $this->assertTrue($comment_type, 'The new comment type has been created.'); // Check that the comment type was created in site default language. @@ -127,14 +130,14 @@ public function testCommentTypeDeletion() { $this->drupalLogin($this->adminUser); // Create a node. - $node = entity_create('node', array( + $node = Node::create(array( 'type' => 'page', 'title' => 'foo', )); $node->save(); // Add a new comment of this type. - $comment = entity_create('comment', array( + $comment = Comment::create(array( 'comment_type' => 'foo', 'entity_type' => 'node', 'field_name' => 'foo', @@ -179,24 +182,4 @@ public function testCommentTypeDeletion() { $this->fail('Exception not thrown.'); } - /** - * Creates a comment comment type (bundle). - * - * @param string $label - * The comment type label. - * - * @return \Drupal\comment\Entity\CommentType - * Created comment type. - */ - protected function createCommentType($label) { - $bundle = entity_create('comment_type', array( - 'id' => $label, - 'label' => $label, - 'description' => '', - 'target_entity_type_id' => 'node', - )); - $bundle->save(); - return $bundle; - } - } diff --git a/core/profiles/standard/config/install/comment.type.comment.yml b/core/profiles/standard/config/install/comment.type.comment.yml index ceef688..e0f028c 100644 --- a/core/profiles/standard/config/install/comment.type.comment.yml +++ b/core/profiles/standard/config/install/comment.type.comment.yml @@ -4,6 +4,3 @@ description: 'Default comment field' target_entity_type_id: node status: true langcode: en -dependencies: - entity: - - comment.type.comment