diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index c244fe4ce8..d9a9ba623c 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\field\Entity\FieldStorageConfig; @@ -195,3 +196,52 @@ function comment_update_8400() { $entity_definition_update_manager = \Drupal::service('entity.definition_update_manager'); $entity_definition_update_manager->updateFieldStorageDefinition($entity_definition_update_manager->getFieldStorageDefinition('status', 'comment')); } + +/** + * Add the revisionable metadata fields to comments. + */ +function comment_update_8401() { + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + + // Add the revisionable metadata fields to the comment entity type. + $entity_type = $definition_update_manager->getEntityType('comment'); + + $revision_metadata_keys = [ + 'revision_user' => 'revision_user', + 'revision_created' => 'revision_created', + 'revision_log_message' => 'revision_log_message' + ]; + $entity_type->set('revision_metadata_keys', $revision_metadata_keys); + + $definition_update_manager->updateEntityType($entity_type); + + // Add the revision metadata fields. + $revision_created = BaseFieldDefinition::create('created') + ->setLabel(t('Revision create time')) + ->setDescription(t('The time that the current revision was created.')) + ->setRevisionable(TRUE); + $definition_update_manager->installFieldStorageDefinition('revision_created', 'comment', 'comment', $revision_created); + + $revision_user = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Revision user')) + ->setDescription(t('The user ID of the author of the current revision.')) + ->setSetting('target_type', 'user') + ->setRevisionable(TRUE); + $definition_update_manager->installFieldStorageDefinition('revision_user', 'comment', 'comment', $revision_user); + + $revision_log_message = BaseFieldDefinition::create('string_long') + ->setLabel(t('Revision log message')) + ->setDescription(t('Briefly describe the changes you have made.')) + ->setRevisionable(TRUE) + ->setDefaultValue('') + ->setDisplayOptions('form', [ + 'type' => 'string_textarea', + 'weight' => 25, + 'settings' => [ + 'rows' => 4, + ], + ]); + $definition_update_manager->installFieldStorageDefinition('revision_log_message', 'comment', 'comment', $revision_log_message); + + return t('Comments have been converted to revisionable.'); +} diff --git a/core/modules/comment/comment.post_update.php b/core/modules/comment/comment.post_update.php new file mode 100644 index 0000000000..b3bba9e32e --- /dev/null +++ b/core/modules/comment/comment.post_update.php @@ -0,0 +1,35 @@ +convertToRevisionable( + $sandbox, + [ + 'subject', + 'name', + 'mail', + 'homepage', + 'hostname', + 'created', + 'changed', + ] + ); +} diff --git a/core/modules/comment/src/CommentStorageSchema.php b/core/modules/comment/src/CommentStorageSchema.php index 2106a8ec2b..d2c87af431 100644 --- a/core/modules/comment/src/CommentStorageSchema.php +++ b/core/modules/comment/src/CommentStorageSchema.php @@ -17,7 +17,7 @@ class CommentStorageSchema extends SqlContentEntityStorageSchema { protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) { $schema = parent::getEntitySchema($entity_type, $reset); - $schema['comment_field_data']['indexes'] += [ + $schema[$entity_type->getDataTable()]['indexes'] += [ 'comment__status_pid' => ['pid', 'status'], 'comment__num_new' => [ 'entity_id', diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 821b30d0ba..c36a05aea2 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -4,10 +4,8 @@ use Drupal\Component\Utility\Number; use Drupal\Core\Cache\Cache; -use Drupal\Core\Entity\ContentEntityBase; use Drupal\comment\CommentInterface; -use Drupal\Core\Entity\EntityChangedTrait; -use Drupal\Core\Entity\EntityPublishedTrait; +use Drupal\Core\Entity\EditorialContentEntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -43,16 +41,24 @@ * }, * base_table = "comment", * data_table = "comment_field_data", + * revision_table = "comment_revision", + * revision_data_table = "comment_field_revision", * uri_callback = "comment_uri", * translatable = TRUE, * entity_keys = { * "id" = "cid", + * "revision" = "revision_id", * "bundle" = "comment_type", * "label" = "subject", * "langcode" = "langcode", * "uuid" = "uuid", * "published" = "status", * }, + * revision_metadata_keys = { + * "revision_user" = "revision_user", + * "revision_created" = "revision_created", + * "revision_log_message" = "revision_log_message", + * }, * links = { * "canonical" = "/comment/{comment}", * "delete-form" = "/comment/{comment}/delete", @@ -66,10 +72,7 @@ * } * ) */ -class Comment extends ContentEntityBase implements CommentInterface { - - use EntityChangedTrait; - use EntityPublishedTrait; +class Comment extends EditorialContentEntityBase implements CommentInterface { /** * The thread for which a lock was acquired. @@ -251,6 +254,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['subject'] = BaseFieldDefinition::create('string') ->setLabel(t('Subject')) ->setTranslatable(TRUE) + ->setRevisionable(TRUE) ->setSetting('max_length', 64) ->setDisplayOptions('form', [ 'type' => 'string_textfield', @@ -270,18 +274,21 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Name')) ->setDescription(t("The comment author's name.")) ->setTranslatable(TRUE) + ->setRevisionable(TRUE) ->setSetting('max_length', 60) ->setDefaultValue(''); $fields['mail'] = BaseFieldDefinition::create('email') ->setLabel(t('Email')) ->setDescription(t("The comment author's email address.")) - ->setTranslatable(TRUE); + ->setTranslatable(TRUE) + ->setRevisionable(TRUE); $fields['homepage'] = BaseFieldDefinition::create('uri') ->setLabel(t('Homepage')) ->setDescription(t("The comment author's home page address.")) ->setTranslatable(TRUE) + ->setRevisionable(TRUE) // URIs are not length limited by RFC 2616, but we can only store 255 // characters in our comment DB schema. ->setSetting('max_length', 255); @@ -290,17 +297,20 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Hostname')) ->setDescription(t("The comment author's hostname.")) ->setTranslatable(TRUE) + ->setRevisionable(TRUE) ->setSetting('max_length', 128); $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) ->setDescription(t('The time that the comment was created.')) - ->setTranslatable(TRUE); + ->setTranslatable(TRUE) + ->setRevisionable(TRUE); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) ->setDescription(t('The time that the comment was last edited.')) - ->setTranslatable(TRUE); + ->setTranslatable(TRUE) + ->setRevisionable(TRUE); $fields['thread'] = BaseFieldDefinition::create('string') ->setLabel(t('Thread place')) diff --git a/core/modules/comment/src/Tests/Update/CommentUpdateTest.php b/core/modules/comment/src/Tests/Update/CommentUpdateTest.php index 3db9c4a67b..8e2a402cf7 100644 --- a/core/modules/comment/src/Tests/Update/CommentUpdateTest.php +++ b/core/modules/comment/src/Tests/Update/CommentUpdateTest.php @@ -3,10 +3,12 @@ namespace Drupal\comment\Tests\Update; use Drupal\system\Tests\Update\UpdatePathTestBase; +use Drupal\user\Entity\User; /** * Tests that comment settings are properly updated during database updates. * + * @group Update * @group comment */ class CommentUpdateTest extends UpdatePathTestBase { @@ -71,4 +73,53 @@ public function testPublishedEntityKey() { $this->assertTrue(\Drupal::database()->schema()->indexExists('comment_field_data', 'comment__status_comment_type')); } + /** + * Tests the conversion of comments to be revisionable. + * + * @see comment_update_8401() + * @see comment_post_update_make_comment_revisionable() + */ + public function testConversionToRevisionableAndPublishable() { + $this->runUpdates(); + + $spanish = \Drupal::languageManager()->getLanguage('es'); + + // Log in as user 1. + $account = User::load(1); + $account->pass_raw = 'drupal'; + $this->drupalLogin($account); + + // Make sure a translated page exists. + $this->drupalGet('node/8', ['language' => $spanish]); + // Check for text of two comments. + $this->assertText('Hola'); + $this->assertText('Hello'); + + // Check that comments can be created, saved and then loaded. + $storage = \Drupal::entityTypeManager()->getStorage('comment'); + /** @var \Drupal\comment\Entity\Comment $comment */ + $comment = $storage->create([ + 'entity_id' => 8, + 'entity_type' => 'node', + 'field_name' => 'comment', + 'subject' => 'Test subject', + 'comment_body' => ['Test comment body'], + ]); + $comment->save(); + + $storage->resetCache(); + $comment = $storage->loadRevision($comment->getRevisionId()); + + $this->assertEqual('Test subject', $comment->label()); + $this->assertEqual('Test comment body', $comment->comment_body->value); + $this->assertTrue($comment->isPublished()); + } + + /** + * {@inheritdoc} + */ + protected function replaceUser1() { + // Do not replace the user from our dump. + } + } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php index ea705de8a4..c5a47ea63b 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php @@ -123,6 +123,9 @@ protected function getExpectedNormalizedEntity() { 'cid' => [ ['value' => 1], ], + 'revision_id' => [ + ['value' => 1], + ], 'uuid' => [ ['value' => $this->entity->uuid()], ], @@ -193,6 +196,11 @@ protected function getExpectedNormalizedEntity() { 'value' => '01/', ], ], + 'revision_created' => [ + $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()), + ], + 'revision_user' => [], + 'revision_log_message' => [], 'comment_body' => [ [ 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',