diff --git a/core/modules/block_content/block_content.info.yml b/core/modules/block_content/block_content.info.yml index d32bda7..b9ae564 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 658eab1..ab2b261 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); +} \ No newline at end of file diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index 2410c7c..8815f56 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 4eefe28..130cae1 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 958fc7a..b0454a9 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -6,8 +6,10 @@ use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Entity\RevisionLogInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\block_content\BlockContentInterface; +use Drupal\user\UserInterface; /** * Defines the custom block entity class. @@ -126,7 +128,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(); } } @@ -204,6 +206,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.')) @@ -218,7 +231,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { * {@inheritdoc} */ public function getRevisionLog() { - return $this->get('revision_log')->value; + return $this->getRevisionLogMessage(); } /** @@ -233,8 +246,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 720443d..216f1ad 100644 --- a/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php +++ b/core/modules/block_content/src/Tests/BlockContentRevisionsTest.php @@ -3,6 +3,7 @@ namespace Drupal\block_content\Tests; use Drupal\block_content\Entity\BlockContent; +use Drupal\user\UserInterface; /** * Create a block with revisions. @@ -43,8 +44,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 +65,15 @@ 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(), ))); + $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 4021032..16df855 100644 --- a/core/modules/block_content/src/Tests/BlockContentTestBase.php +++ b/core/modules/block_content/src/Tests/BlockContentTestBase.php @@ -19,7 +19,7 @@ /** * Admin user * - * @var object + * @var \Drupal\user\UserInterface */ protected $adminUser; diff --git a/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php b/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php index 306d102..c52e3ea 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' ]; /**