diff --git a/core/modules/block_content/block_content.info.yml b/core/modules/block_content/block_content.info.yml index d32bda79aabe415f0eda034b6009482a1d0f3c23..b9ae5645c092e91f7d23c2741ad08ab038c76b7e 100644 --- a/core/modules/block_content/block_content.info.yml +++ b/core/modules/block_content/block_content.info.yml @@ -7,4 +7,5 @@ core: 8.x dependencies: - block - text + - user configure: entity.block_content.collection diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 658eab16bdbe8ee2bfa915afed63d36225b3d6b1..4279cdef722865bf0c436173aae2a581a350a94a 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -39,3 +39,25 @@ function block_content_update_8002() { // are stable. // @see https://www.drupal.org/node/2569469 } + +/** + * Add 'revision_created' and 'revision_user' fields to 'block_content' entities. + */ +function block_content_update_8003() { + $revision_created = BaseFieldDefinition::create('created') + ->setLabel(t('Revision create time')) + ->setDescription(t('The time that the current revision was created.')) + ->setRevisionable(TRUE); + + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('revision_created', 'block_content', 'block_content', $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); + + \Drupal::entityDefinitionUpdateManager() + ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user); +} diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index 2410c7ce15c67b27398dea70f02e56735c71eedc..8815f56526997730a91c3b072e1f0c4a6fba5ad2 100644 --- a/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -88,7 +88,7 @@ protected function prepareEntity() { // Set up default values, if required. $block_type = $this->blockContentTypeStorage->load($block->bundle()); if (!$block->isNew()) { - $block->setRevisionLog(NULL); + $block->setRevisionLogMessage(NULL); } // Always use the default revision setting. $block->setNewRevision($block_type->shouldCreateNewRevision()); @@ -170,6 +170,9 @@ public function save(array $form, FormStateInterface $form_state) { // Save as a new revision if requested to do so. if (!$form_state->isValueEmpty('revision')) { $block->setNewRevision(); + // If a new revision is created, save the current user as revision author. + $block->setRevisionCreationTime(REQUEST_TIME); + $block->setRevisionUserId(\Drupal::currentUser()->id()); } $insert = $block->isNew(); diff --git a/core/modules/block_content/src/BlockContentInterface.php b/core/modules/block_content/src/BlockContentInterface.php index 4eefe28a954061a7043a777281d48c62102ed700..130cae1c3cfbd5b2d43ae96b93ea72b41eafafb9 100644 --- a/core/modules/block_content/src/BlockContentInterface.php +++ b/core/modules/block_content/src/BlockContentInterface.php @@ -4,17 +4,21 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityChangedInterface; +use Drupal\Core\Entity\RevisionLogInterface; /** * Provides an interface defining a custom block entity. */ -interface BlockContentInterface extends ContentEntityInterface, EntityChangedInterface { +interface BlockContentInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface { /** * Returns the block revision log message. * * @return string * The revision log message. + * + * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Entity\RevisionLogInterface::getRevisionLogMessage() instead. */ public function getRevisionLog(); @@ -37,6 +41,9 @@ public function setInfo($info); * * @return \Drupal\block_content\BlockContentInterface * The class instance that this method is called on. + * + * @deprecated in Drupal 8.2.0, will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Entity\RevisionLogInterface::setRevisionLogMessage() instead. */ public function setRevisionLog($revision_log); diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 8c22644d7392720273694c2c064cf26cbdd6ac7d..9e0531e6da798ddce0477d804e1a25b5a45fedb8 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\block_content\BlockContentInterface; +use Drupal\user\UserInterface; /** * Defines the custom block entity class. @@ -126,7 +127,7 @@ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $reco // If we are updating an existing block_content without adding a new // revision and the user did not supply a revision log, keep the existing // one. - $record->revision_log = $this->original->getRevisionLog(); + $record->revision_log = $this->original->getRevisionLogMessage(); } } @@ -183,6 +184,17 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setTranslatable(TRUE) ->setRevisionable(TRUE); + $fields['revision_created'] = BaseFieldDefinition::create('created') + ->setLabel(t('Revision create time')) + ->setDescription(t('The time that the current revision was created.')) + ->setRevisionable(TRUE); + + $fields['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); + $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Revision translation affected')) ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) @@ -197,7 +209,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { * {@inheritdoc} */ public function getRevisionLog() { - return $this->get('revision_log')->value; + return $this->getRevisionLogMessage(); } /** @@ -212,7 +224,63 @@ public function setInfo($info) { * {@inheritdoc} */ public function setRevisionLog($revision_log) { - $this->set('revision_log', $revision_log); + return $this->setRevisionLogMessage($revision_log); + } + + /** + * {@inheritdoc} + */ + public function getRevisionCreationTime() { + return $this->get('revision_created')->value; + } + + /** + * {@inheritdoc} + */ + public function setRevisionCreationTime($timestamp) { + $this->set('revision_created', $timestamp); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionUser() { + return $this->get('revision_user')->entity; + } + + public function setRevisionUser(UserInterface $account) { + $this->set('revision_user', $account); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionUserId() { + return $this->get('revision_user')->entity->id(); + } + + /** + * {@inheritdoc} + */ + public function setRevisionUserId($user_id) { + $this->set('revision_user', $user_id); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionLogMessage() { + return $this->get('revision_log')->value; + } + + /** + * {@inheritdoc} + */ + public function setRevisionLogMessage($revision_log_message) { + $this->set('revision_log', $revision_log_message); return $this; } diff --git a/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php b/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php index 720443d5fca302dbcf9a44a197a0f3392c5384cc..cc08654fbe6669052b53ca6f3449b1fbf4967e9f 100644 --- a/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php +++ b/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php @@ -3,6 +3,8 @@ namespace Drupal\block_content\Tests; use Drupal\block_content\Entity\BlockContent; +use Drupal\user\Entity\User; +use Drupal\user\UserInterface; /** * Create a block with revisions. @@ -29,6 +31,9 @@ class BlockContentRevisionsTest extends BlockContentTestBase { protected function setUp() { parent::setUp(); + /** @var UserInterface $user */ + $user = User::load(1); + // Create initial block. $block = $this->createBlockContent('initial'); @@ -43,8 +48,10 @@ protected function setUp() { $revision_count = 3; for ($i = 0; $i < $revision_count; $i++) { $block->setNewRevision(TRUE); - $block->setRevisionLog($this->randomMachineName(32)); - $logs[] = $block->getRevisionLog(); + $block->setRevisionLogMessage($this->randomMachineName(32)); + $block->setRevisionUser($this->adminUser); + $block->setRevisionCreationTime(REQUEST_TIME); + $logs[] = $block->getRevisionLogMessage(); $block->save(); $blocks[] = $block->getRevisionId(); } @@ -62,11 +69,17 @@ public function testRevisions() { foreach ($blocks as $delta => $revision_id) { // Confirm the correct revision text appears. + /** @var \Drupal\block_content\BlockContentInterface $loaded */ $loaded = entity_revision_load('block_content', $revision_id); // Verify revision log is the same. - $this->assertEqual($loaded->getRevisionLog(), $logs[$delta], format_string('Correct log message found for revision @revision', array( + $this->assertEqual($loaded->getRevisionLogMessage(), $logs[$delta], format_string('Correct log message found for revision @revision', array( '@revision' => $loaded->getRevisionId(), ))); + if ($delta > 0) { + $this->assertTrue($loaded->getRevisionUser() instanceof UserInterface, 'Revision User found.'); + $this->assertTrue(is_numeric($loaded->getRevisionUserId()), 'Revision User ID found.'); + $this->assertTrue(is_numeric($loaded->getRevisionCreationTime()), 'Revision time found.'); + } } // Confirm that this is the default revision. diff --git a/core/modules/block_content/src/Tests/BlockContentTestBase.php b/core/modules/block_content/src/Tests/BlockContentTestBase.php index 402103253962cace04f075261199e62df744580f..16df8558ede0596793c0d97ad23fa789c46791ea 100644 --- a/core/modules/block_content/src/Tests/BlockContentTestBase.php +++ b/core/modules/block_content/src/Tests/BlockContentTestBase.php @@ -19,7 +19,7 @@ abstract class BlockContentTestBase extends WebTestBase { /** * Admin user * - * @var object + * @var \Drupal\user\UserInterface */ protected $adminUser; diff --git a/core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php b/core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php new file mode 100644 index 0000000000000000000000000000000000000000..ab59eba282aa6330108433c8cef64eb422b0c02e --- /dev/null +++ b/core/modules/block_content/src/Tests/BlockContentUpdateEntityFields.php @@ -0,0 +1,39 @@ +<?php + +namespace Drupal\block_content\Tests; + +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\system\Tests\Update\UpdatePathTestBase; + +/** + * Tests adding revision_user and revision_created fields. + * + * @group Update + */ +class BlockContentUpdateEntityFields extends UpdatePathTestBase { + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles() { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz', + ]; + } + + /** + * Tests that email token in status_blocked of user.mail is updated. + */ + public function testAddingFields() { + $this->runUpdates(); + + $post_revision_created = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('revision_created', 'block_content'); + $post_revision_user = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('revision_user', 'block_content'); + $this->assertTrue($post_revision_created instanceof BaseFieldDefinition, "Revision created field found"); + $this->assertTrue($post_revision_user instanceof BaseFieldDefinition, "Revision user field found"); + + $this->assertEqual('created', $post_revision_created->getType(), "Field is type created"); + $this->assertEqual('entity_reference', $post_revision_user->getType(), "Field is type entity_reference"); + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php b/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php index 306d102d7f307be7fbfba3749455aec9d0aa0d40..c52e3ea8533abab32a63fce7a58a0b001236eeae 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php @@ -21,6 +21,7 @@ class CacheabilityMetadataConfigOverrideTest extends KernelTestBase { 'config', 'config_override_test', 'system', + 'user' ]; /**