diff --git a/src/Revision/EnhancedEntityRevisionTrait.php b/src/Revision/EnhancedEntityRevisionTrait.php deleted file mode 100644 index c26338c178e7247636eca9f8ff84ccaf97b41ec7..0000000000000000000000000000000000000000 --- a/src/Revision/EnhancedEntityRevisionTrait.php +++ /dev/null @@ -1,86 +0,0 @@ -<?php - -/** - * @file - * Contains \Drupal\entity\Revision\EnhancedEntityRevisionTrait. - */ - -namespace Drupal\entity\Revision; -use Drupal\user\Entity\User; - -/** - * Provides a trait implementing \Drupal\entity\EnhanceredEntityRevisionInterface. - */ -trait EnhancedEntityRevisionTrait { - - /** - * Returns whether the entity type has a specific field. - * - * @param string $field_name - * The field name. - * - * @return bool - */ - abstract function hasField($field_name); - - /** - * {@inheritdoc} - */ - public function getRevisionCreationTime() { - if ($this->hasField('revision_create')) { - return $this->revision_create->value; - } - return 0; - } - - /** - * {@inheritdoc} - */ - public function setRevisionCreationTime($timestamp) { - if ($this->hasField('revision_create')) { - $this->revision_create->value = $timestamp; - } - return $this; - } - - /** - * {@inheritdoc} - */ - public function getRevisionAuthor() { - if ($this->hasField('revision_author')) { - return $this->revision_author->entity; - } - return User::getAnonymousUser(); - } - - /** - * {@inheritdoc} - */ - public function setRevisionAuthorId($uid) { - if ($this->hasField('revision_author')) { - $this->revision_author->target_id = $uid; - } - return $this; - } - - /** - * {@inheritdoc} - */ - public function getRevisionLog() { - if ($this->hasField('revision_log')) { - return $this->revision_log->value; - } - return ''; - } - - /** - * {@inheritdoc} - */ - public function setRevisionLog($revision_log) { - if ($this->hasField('revision_log')) { - $this->revision_log->value = $revision_log; - } - return $this; - } - -} diff --git a/src/Revision/EnhancedEntityRevisionInterface.php b/src/Revision/EntityRevisionLogInterface.php similarity index 62% rename from src/Revision/EnhancedEntityRevisionInterface.php rename to src/Revision/EntityRevisionLogInterface.php index 72fbf303d387d05a73c2b2cc0de790e8afe71a25..3a1520334b3795d95dcf5fc988a1d4396166967c 100644 --- a/src/Revision/EnhancedEntityRevisionInterface.php +++ b/src/Revision/EntityRevisionLogInterface.php @@ -2,18 +2,18 @@ /** * @file - * Contains \Drupal\entity\Revision\EnhancedEntityRevisionInterface. + * Contains \Drupal\entity\Revision\EntityRevisionLogInterface. */ namespace Drupal\entity\Revision; /** - * Defines an entity type with create/author/log information for revisions. + * Defines an entity type with create time/author/log information for revisions. */ -interface EnhancedEntityRevisionInterface { +interface EntityRevisionLogInterface { /** - * Gets the node revision creation timestamp. + * Gets the entity revision creation timestamp. * * @return int|NULL * The UNIX timestamp of when this revision was created. Return NULL if the @@ -22,7 +22,7 @@ interface EnhancedEntityRevisionInterface { public function getRevisionCreationTime(); /** - * Sets the node revision creation timestamp. + * Sets the entity revision creation timestamp. * * @param int $timestamp * The UNIX timestamp of when this revision was created. @@ -32,43 +32,41 @@ interface EnhancedEntityRevisionInterface { public function setRevisionCreationTime($timestamp); /** - * Gets the node revision author. + * Gets the entity revision author. * * @return \Drupal\user\UserInterface|NULL * The user entity for the revision author. Return NULL if the entity type * doesn't support revision authors. */ - public function getRevisionAuthor(); + public function getRevisionUser(); /** - * Sets the node revision author. + * Sets the entity revision author. * - * @param int $uid + * @param int $user_id * The user ID of the revision author. * * @return $this */ - public function setRevisionAuthorId($uid); + public function setRevisionUser($user_id); /** - * @todo Ideally this would be its own interface? - * * Returns the entity revision log message. * * @return string|NULL * The revision log message. Return NULL if the entity type doesn't support * revision logs. */ - public function getRevisionLog(); + public function getRevisionLogMessage(); /** * Sets the entity revision log message. * - * @param string $revision_log + * @param string $revision_log_message * The revision log message. * * @return $this */ - public function setRevisionLog($revision_log); + public function setRevisionLogMessage($revision_log_message); } diff --git a/src/Revision/EntityRevisionLogTrait.php b/src/Revision/EntityRevisionLogTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..a175931a7548783522012a947d491e409f2c7e9f --- /dev/null +++ b/src/Revision/EntityRevisionLogTrait.php @@ -0,0 +1,122 @@ +<?php + +/** + * @file + * Contains \Drupal\entity\Revision\EntityRevisionLogTrait. + */ + +namespace Drupal\entity\Revision; +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\user\Entity\User; + +/** + * Provides a trait implementing \Drupal\entity\Revision\EntityRevisionLogInterface. + */ +trait EntityRevisionLogTrait { + + /** + * Returns whether the entity type has a specific field. + * + * @param string $field_name + * The field name. + * + * @return bool + */ + abstract function hasField($field_name); + + /** + * Provides the base fields for the entity revision log trait. + * + * @return \Drupal\Core\Field\BaseFieldDefinition[] + */ + protected static function enhancedEntityRevisionDefaultBaseFields() { + $fields = []; + + $fields['revision_create'] = 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; + } + + /** + * {@inheritdoc} + */ + public function getRevisionCreationTime() { + if ($this->hasField('revision_create')) { + return $this->revision_create->value; + } + return 0; + } + + /** + * {@inheritdoc} + */ + public function setRevisionCreationTime($timestamp) { + if ($this->hasField('revision_create')) { + $this->revision_create->value = $timestamp; + } + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionUser() { + if ($this->hasField('revision_user')) { + return $this->revision_user->entity; + } + return User::getAnonymousUser(); + } + + /** + * {@inheritdoc} + */ + public function setRevisionUser($user_id) { + if ($this->hasField('revision_user')) { + $this->revision_user->target_id = $user_id; + } + return $this; + } + + /** + * {@inheritdoc} + */ + public function getRevisionLogMessage() { + if ($this->hasField('revision_log_message')) { + return $this->revision_log_message->value; + } + return ''; + } + + /** + * {@inheritdoc} + */ + public function setRevisionLogMessage($revision_log_message) { + if ($this->hasField('revision_log_message')) { + $this->revision_log_message->value = $revision_log_message; + } + return $this; + } + +} diff --git a/tests/Kernel/EntityRevisionLogTraitTest.php b/tests/Kernel/EntityRevisionLogTraitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..ad9c94373fb5f86976fa4bd460452e517faf50e2 --- /dev/null +++ b/tests/Kernel/EntityRevisionLogTraitTest.php @@ -0,0 +1,63 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\entity\Kernel\EntityRevisionLogTraitTest. + */ + +namespace Drupal\Tests\entity\Kernel; + +use Drupal\entity_test\Entity\EntityWithRevisionLog; +use Drupal\KernelTests\KernelTestBase; +use Drupal\user\Entity\User; + +/** + * @coversDefaultClass \Drupal\entity\Revision\EntityRevisionLogTrait + */ +class EntityRevisionLogTraitTest extends KernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['entity', 'entity_test', 'user', 'system']; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->installEntitySchema('user'); + $this->installSchema('system', 'sequences'); + } + + public function testEntityRevisionLog() { + $user = User::create([ + 'name' => 'user', + ]); + $user->save(); + $user2 = User::create([ + 'name' => 'user2', + ]); + $user2->save(); + + /** @var \Drupal\entity\Revision\EntityRevisionLogInterface $entity */ + $entity = EntityWithRevisionLog::create([ + 'revision_user' => $user->id(), + 'revision_create' => 1447941735, + 'revision_log_message' => 'Test message', + ]); + + $this->assertEquals(1447941735, $entity->getRevisionCreationTime()); + $this->assertEquals($user->id(), $entity->getRevisionUser()->id()); + $this->assertEquals('Test message', $entity->getRevisionLogMessage()); + + $entity->setRevisionCreationTime(1234567890); + $this->assertEquals(1234567890, $entity->getRevisionCreationTime()); + $entity->setRevisionUser($user2->id()); + $this->assertEquals($user2->id(), $entity->getRevisionUser()->id()); + $entity->setRevisionLogMessage('Giraffe!'); + $this->assertEquals('Giraffe!', $entity->getRevisionLogMessage()); + } + +} diff --git a/tests/modules/entity_test/entity_test.info.yml b/tests/modules/entity_test/entity_test.info.yml new file mode 100644 index 0000000000000000000000000000000000000000..83afba239e59c1bc0c16200a7e36b43e35780448 --- /dev/null +++ b/tests/modules/entity_test/entity_test.info.yml @@ -0,0 +1,3 @@ +name: Entity test +type: module +core: 8.x diff --git a/tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php b/tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php new file mode 100644 index 0000000000000000000000000000000000000000..b29082635b69c685309c479a6f12778cd9ce6b81 --- /dev/null +++ b/tests/modules/entity_test/src/Entity/EntityWithRevisionLog.php @@ -0,0 +1,72 @@ +<?php + +/** + * @file + * Contains \Drupal\entity_test\Entity\EntityWithRevisionLog. + */ + +namespace Drupal\entity_test\Entity; + +use Drupal\Core\Entity\ContentEntityBase; +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\entity\Revision\EntityRevisionLogTrait; + +/** + * @ContentEntityType( + * id = "entity_test__revision_log", + * label = @Translation("Entity test with revision log"), + * handlers = { + * "storage" = "\Drupal\Core\Entity\Sql\SqlContentEntityStorage", + * }, + * translatable = TRUE, + * revisionable = TRUE, + * entity_keys = { + * "id" = "id", + * "revision" = "vid", + * "langcode" = "langcode", + * } + * ) + */ +class EntityWithRevisionLog extends ContentEntityBase { + + use EntityRevisionLogTrait; + + /** + * {@inheritdoc} + */ + public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { + $fields = []; + + $fields['id'] = BaseFieldDefinition::create('integer') + ->setLabel(t('')) + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); + + $fields['uuid'] = BaseFieldDefinition::create('uuid') + ->setLabel(t('UUID')) + ->setReadOnly(TRUE); + + $fields['vid'] = BaseFieldDefinition::create('integer') + ->setLabel(t('Revision ID')) + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); + + $fields['langcode'] = BaseFieldDefinition::create('language') + ->setLabel(t('Language')) + ->setTranslatable(TRUE) + ->setRevisionable(TRUE) + ->setDisplayOptions('view', [ + 'type' => 'hidden', + ]) + ->setDisplayOptions('form', [ + 'type' => 'language_select', + 'weight' => 2, + ]); + + $fields += static::enhancedEntityRevisionDefaultBaseFields(); + + return $fields; + } + +}