Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php
- 9 core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php
Namespace
Drupal\Core\Entity
File
core/lib/Drupal/Core/Entity/RevisionLogEntityTrait.php
View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
trait RevisionLogEntityTrait {
public static function revisionLogBaseFieldDefinitions(EntityTypeInterface $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface) {
throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type
->id() . ' is not a content entity type.');
}
foreach ([
'revision_created',
'revision_user',
'revision_log_message',
] as $revision_metadata_key) {
if (!$entity_type
->hasRevisionMetadataKey($revision_metadata_key)) {
throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type
->id() . ' does not have an "' . $revision_metadata_key . '" entity revision metadata key.');
}
}
$fields[$entity_type
->getRevisionMetadataKey('revision_created')] = BaseFieldDefinition::create('created')
->setLabel(t('Revision create time'))
->setDescription(t('The time that the current revision was created.'))
->setRevisionable(TRUE);
$fields[$entity_type
->getRevisionMetadataKey('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[$entity_type
->getRevisionMetadataKey('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;
}
public function getRevisionCreationTime() {
return $this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_created')}->value;
}
public function setRevisionCreationTime($timestamp) {
$this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_created')}->value = $timestamp;
return $this;
}
public function getRevisionUser() {
return $this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_user')}->entity;
}
public function setRevisionUser(UserInterface $account) {
$this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_user')}->entity = $account;
return $this;
}
public function getRevisionUserId() {
return $this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_user')}->target_id;
}
public function setRevisionUserId($user_id) {
$this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_user')}->target_id = $user_id;
return $this;
}
public function getRevisionLogMessage() {
return $this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_log_message')}->value;
}
public function setRevisionLogMessage($revision_log_message) {
$this->{$this
->getEntityType()
->getRevisionMetadataKey('revision_log_message')}->value = $revision_log_message;
return $this;
}
public abstract function getEntityType();
}
Traits