Commit 6315ac43 authored by catch's avatar catch

Issue #2666808 by dawehner, amateescu: Add a revision log interface

parent b8c2f8f2
<?php
/**
* @file
* Contains \Drupal\Core\Entity\RevisionLogEntityTrait.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
/**
* Provides a trait for accessing revision logging and ownership information.
*
* @ingroup entity_api
*/
trait RevisionLogEntityTrait {
/**
* Provides revision-related base field definitions for an entity type.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface[]
* An array of base field definitions for the entity type, keyed by field
* name.
*
* @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()
*/
public static function revisionLogBaseFieldDefinitions(EntityTypeInterface $entity_type) {
$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_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,
],
]);
return $fields;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionCreationTime().
*/
public function getRevisionCreationTime() {
return $this->revision_created->value;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionCreationTime().
*/
public function setRevisionCreationTime($timestamp) {
$this->revision_created->value = $timestamp;
return $this;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUser().
*/
public function getRevisionUser() {
return $this->revision_user->entity;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUser().
*/
public function setRevisionUser(UserInterface $account) {
$this->revision_user->entity = $account;
return $this;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUserId().
*/
public function getRevisionUserId() {
return $this->revision_user->target_id;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUserId().
*/
public function setRevisionUserId($user_id) {
$this->revision_user->target_id = $user_id;
return $this;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionLogMessage().
*/
public function getRevisionLogMessage() {
return $this->revision_log_message->value;
}
/**
* Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionLogMessage().
*/
public function setRevisionLogMessage($revision_log_message) {
$this->revision_log_message->value = $revision_log_message;
return $this;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\RevisionLogInterface.
*/
namespace Drupal\Core\Entity;
use Drupal\user\UserInterface;
/**
* Defines methods for an entity that supports revision logging and ownership.
*/
interface RevisionLogInterface extends RevisionableInterface {
/**
* Gets the entity revision creation timestamp.
*
* @return int
* The UNIX timestamp of when this revision was created.
*/
public function getRevisionCreationTime();
/**
* Sets the entity revision creation timestamp.
*
* @param int $timestamp
* The UNIX timestamp of when this revision was created.
*
* @return $this
*/
public function setRevisionCreationTime($timestamp);
/**
* Gets the entity revision author.
*
* @return \Drupal\user\UserInterface
* The user entity for the revision author.
*/
public function getRevisionUser();
/**
* Sets the entity revision author.
*
* @param \Drupal\user\UserInterface $account
* The user account of the revision author.
*
* @return $this
*/
public function setRevisionUser(UserInterface $account);
/**
* Gets the entity revision author ID.
*
* @return int
* The user ID.
*/
public function getRevisionUserId();
/**
* Sets the entity revision author by ID.
*
* @param int $user_id
* The user ID of the revision author.
*
* @return $this
*/
public function setRevisionUserId($user_id);
/**
* Returns the entity revision log message.
*
* @return string
* The revision log message.
*/
public function getRevisionLogMessage();
/**
* Sets the entity revision log message.
*
* @param string $revision_log_message
* The revision log message.
*
* @return $this
*/
public function setRevisionLogMessage($revision_log_message);
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\RevisionableContentEntityBase.
*/
namespace Drupal\Core\Entity;
/**
* Provides a content entity with extended support for revisions.
*
* In addition to the parent entity class, base fields and methods for
* accessing the revision log message, revision owner and the revision creation
* time are provided.
*
* @ingroup entity_api
*/
abstract class RevisionableContentEntityBase extends ContentEntityBase implements RevisionLogInterface {
use RevisionLogEntityTrait;
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::revisionLogBaseFieldDefinitions($entity_type);
return $fields;
}
}
<?php
/**
* @file
* Contains \Drupal\entity_test\Entity\EntityTestWithRevisionLog.
*/
namespace Drupal\entity_test\Entity;
use Drupal\Core\Entity\RevisionableContentEntityBase;
/**
* Defines the test entity class.
*
* @ContentEntityType(
* id = "entity_test_revlog",
* label = @Translation("Test entity - revisions log"),
* handlers = {
* "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
* "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
* "form" = {
* "default" = "Drupal\entity_test\EntityTestForm",
* "delete" = "Drupal\entity_test\EntityTestDeleteForm"
* },
* "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
* "translation" = "Drupal\content_translation\ContentTranslationHandler",
* "views_data" = "Drupal\views\EntityViewsData",
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* },
* base_table = "entity_test_revlog",
* revision_table = "entity_test_revlog_revision",
* admin_permission = "administer entity_test content",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "revision" = "revision_id",
* "bundle" = "type",
* "label" = "name",
* "langcode" = "langcode",
* },
* links = {
* "canonical" = "/entity_test_revlog/manage/{entity_test_revlog}",
* "delete-form" = "/entity_test/delete/entity_test_revlog/{entity_test_revlog}",
* "edit-form" = "/entity_test_revlog/manage/{entity_test_revlog}/edit",
* "revision" = "/entity_test_revlog/{entity_test_revlog}/revision/{entity_test_revlog_revision}/view",
* }
* )
*/
class EntityTestWithRevisionLog extends RevisionableContentEntityBase {
}
<?php
/**
* @file
* Contains \Drupal\KernelTests\Core\Entity\RevisionableContentEntityBaseTest.
*/
namespace Drupal\KernelTests\Core\Entity;
use Drupal\entity_test\Entity\EntityTestWithRevisionLog;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
/**
* @coversDefaultClass \Drupal\Core\Entity\RevisionableContentEntityBase
* @group Entity
*/
class RevisionableContentEntityBaseTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test', 'system', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('entity_test_revlog');
$this->installEntitySchema('user');
$this->installSchema('system', 'sequences');
}
public function testRevisionableContentEntity() {
$user = User::create(['name' => 'test name']);
$user->save();
/** @var \Drupal\entity_test\Entity\EntityTestWithRevisionLog $entity */
$entity = EntityTestWithRevisionLog::create([
'type' => 'entity_test_revlog',
]);
$entity->save();
$entity->setNewRevision(TRUE);
$random_timestamp = rand(1e8, 2e8);
$entity->setRevisionCreationTime($random_timestamp);
$entity->setRevisionUserId($user->id());
$entity->setRevisionLogMessage('This is my log message');
$entity->save();
$revision_id = $entity->getRevisionId();
$entity = \Drupal::entityTypeManager()->getStorage('entity_test_revlog')->loadRevision($revision_id);
$this->assertEquals($random_timestamp, $entity->getRevisionCreationTime());
$this->assertEquals($user->id(), $entity->getRevisionUserId());
$this->assertEquals($user->id(), $entity->getRevisionUser()->id());
$this->assertEquals('This is my log message', $entity->getRevisionLogMessage());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment