Issue #2716081 by timmillwood, dixon_: BlockContent should have revision_user and revision_created fields and implement RevisionLogInterface

8.2.x
Alex Pott 2016-05-17 12:07:32 -05:00
parent ee5274af83
commit 4ac8f969a7
9 changed files with 163 additions and 9 deletions

View File

@ -7,4 +7,5 @@ core: 8.x
dependencies: dependencies:
- block - block
- text - text
- user
configure: entity.block_content.collection configure: entity.block_content.collection

View File

@ -39,3 +39,25 @@ function block_content_update_8002() {
// are stable. // are stable.
// @see https://www.drupal.org/node/2569469 // @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);
}

View File

@ -88,7 +88,7 @@ class BlockContentForm extends ContentEntityForm {
// Set up default values, if required. // Set up default values, if required.
$block_type = $this->blockContentTypeStorage->load($block->bundle()); $block_type = $this->blockContentTypeStorage->load($block->bundle());
if (!$block->isNew()) { if (!$block->isNew()) {
$block->setRevisionLog(NULL); $block->setRevisionLogMessage(NULL);
} }
// Always use the default revision setting. // Always use the default revision setting.
$block->setNewRevision($block_type->shouldCreateNewRevision()); $block->setNewRevision($block_type->shouldCreateNewRevision());
@ -170,6 +170,9 @@ class BlockContentForm extends ContentEntityForm {
// Save as a new revision if requested to do so. // Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('revision')) { if (!$form_state->isValueEmpty('revision')) {
$block->setNewRevision(); $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(); $insert = $block->isNew();

View File

@ -4,17 +4,21 @@ namespace Drupal\block_content;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\RevisionLogInterface;
/** /**
* Provides an interface defining a custom block entity. * 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. * Returns the block revision log message.
* *
* @return string * @return string
* The revision log message. * 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(); public function getRevisionLog();
@ -37,6 +41,9 @@ interface BlockContentInterface extends ContentEntityInterface, EntityChangedInt
* *
* @return \Drupal\block_content\BlockContentInterface * @return \Drupal\block_content\BlockContentInterface
* The class instance that this method is called on. * 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); public function setRevisionLog($revision_log);

View File

@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\block_content\BlockContentInterface; use Drupal\block_content\BlockContentInterface;
use Drupal\user\UserInterface;
/** /**
* Defines the custom block entity class. * Defines the custom block entity class.
@ -126,7 +127,7 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
// If we are updating an existing block_content without adding a new // 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 // revision and the user did not supply a revision log, keep the existing
// one. // one.
$record->revision_log = $this->original->getRevisionLog(); $record->revision_log = $this->original->getRevisionLogMessage();
} }
} }
@ -183,6 +184,17 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
->setTranslatable(TRUE) ->setTranslatable(TRUE)
->setRevisionable(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') $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected')) ->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
@ -197,7 +209,7 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getRevisionLog() { public function getRevisionLog() {
return $this->get('revision_log')->value; return $this->getRevisionLogMessage();
} }
/** /**
@ -212,7 +224,63 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setRevisionLog($revision_log) { 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; return $this;
} }

View File

@ -3,6 +3,8 @@
namespace Drupal\block_content\Tests; namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent; use Drupal\block_content\Entity\BlockContent;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/** /**
* Create a block with revisions. * Create a block with revisions.
@ -29,6 +31,9 @@ class BlockContentRevisionsTest extends BlockContentTestBase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
/** @var UserInterface $user */
$user = User::load(1);
// Create initial block. // Create initial block.
$block = $this->createBlockContent('initial'); $block = $this->createBlockContent('initial');
@ -43,8 +48,10 @@ class BlockContentRevisionsTest extends BlockContentTestBase {
$revision_count = 3; $revision_count = 3;
for ($i = 0; $i < $revision_count; $i++) { for ($i = 0; $i < $revision_count; $i++) {
$block->setNewRevision(TRUE); $block->setNewRevision(TRUE);
$block->setRevisionLog($this->randomMachineName(32)); $block->setRevisionLogMessage($this->randomMachineName(32));
$logs[] = $block->getRevisionLog(); $block->setRevisionUser($this->adminUser);
$block->setRevisionCreationTime(REQUEST_TIME);
$logs[] = $block->getRevisionLogMessage();
$block->save(); $block->save();
$blocks[] = $block->getRevisionId(); $blocks[] = $block->getRevisionId();
} }
@ -62,11 +69,17 @@ class BlockContentRevisionsTest extends BlockContentTestBase {
foreach ($blocks as $delta => $revision_id) { foreach ($blocks as $delta => $revision_id) {
// Confirm the correct revision text appears. // Confirm the correct revision text appears.
/** @var \Drupal\block_content\BlockContentInterface $loaded */
$loaded = entity_revision_load('block_content', $revision_id); $loaded = entity_revision_load('block_content', $revision_id);
// Verify revision log is the same. // 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(), '@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. // Confirm that this is the default revision.

View File

@ -19,7 +19,7 @@ abstract class BlockContentTestBase extends WebTestBase {
/** /**
* Admin user * Admin user
* *
* @var object * @var \Drupal\user\UserInterface
*/ */
protected $adminUser; protected $adminUser;

View File

@ -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");
}
}

View File

@ -21,6 +21,7 @@ class CacheabilityMetadataConfigOverrideTest extends KernelTestBase {
'config', 'config',
'config_override_test', 'config_override_test',
'system', 'system',
'user'
]; ];
/** /**