RevisionLogEntityTrait.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Field\BaseFieldDefinition;
  4. use Drupal\user\UserInterface;
  5. /**
  6. * Provides a trait for accessing revision logging and ownership information.
  7. *
  8. * @ingroup entity_api
  9. */
  10. trait RevisionLogEntityTrait {
  11. /**
  12. * Provides revision-related base field definitions for an entity type.
  13. *
  14. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  15. * The entity type definition.
  16. *
  17. * @return \Drupal\Core\Field\FieldDefinitionInterface[]
  18. * An array of base field definitions for the entity type, keyed by field
  19. * name.
  20. *
  21. * @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()
  22. */
  23. public static function revisionLogBaseFieldDefinitions(EntityTypeInterface $entity_type) {
  24. $fields[static::getRevisionMetadataKey($entity_type, 'revision_created')] = BaseFieldDefinition::create('created')
  25. ->setLabel(t('Revision create time'))
  26. ->setDescription(t('The time that the current revision was created.'))
  27. ->setRevisionable(TRUE);
  28. $fields[static::getRevisionMetadataKey($entity_type, 'revision_user')] = BaseFieldDefinition::create('entity_reference')
  29. ->setLabel(t('Revision user'))
  30. ->setDescription(t('The user ID of the author of the current revision.'))
  31. ->setSetting('target_type', 'user')
  32. ->setRevisionable(TRUE);
  33. $fields[static::getRevisionMetadataKey($entity_type, 'revision_log_message')] = BaseFieldDefinition::create('string_long')
  34. ->setLabel(t('Revision log message'))
  35. ->setDescription(t('Briefly describe the changes you have made.'))
  36. ->setRevisionable(TRUE)
  37. ->setDefaultValue('')
  38. ->setDisplayOptions('form', [
  39. 'type' => 'string_textarea',
  40. 'weight' => 25,
  41. 'settings' => [
  42. 'rows' => 4,
  43. ],
  44. ]);
  45. return $fields;
  46. }
  47. /**
  48. * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionCreationTime().
  49. */
  50. public function getRevisionCreationTime() {
  51. return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_created')}->value;
  52. }
  53. /**
  54. * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionCreationTime().
  55. */
  56. public function setRevisionCreationTime($timestamp) {
  57. $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_created')}->value = $timestamp;
  58. return $this;
  59. }
  60. /**
  61. * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUser().
  62. */
  63. public function getRevisionUser() {
  64. return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->entity;
  65. }
  66. /**
  67. * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUser().
  68. */
  69. public function setRevisionUser(UserInterface $account) {
  70. $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->entity = $account;
  71. return $this;
  72. }
  73. /**
  74. * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUserId().
  75. */
  76. public function getRevisionUserId() {
  77. return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->target_id;
  78. }
  79. /**
  80. * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUserId().
  81. */
  82. public function setRevisionUserId($user_id) {
  83. $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_user')}->target_id = $user_id;
  84. return $this;
  85. }
  86. /**
  87. * Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionLogMessage().
  88. */
  89. public function getRevisionLogMessage() {
  90. return $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_log_message')}->value;
  91. }
  92. /**
  93. * Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionLogMessage().
  94. */
  95. public function setRevisionLogMessage($revision_log_message) {
  96. $this->{static::getRevisionMetadataKey($this->getEntityType(), 'revision_log_message')}->value = $revision_log_message;
  97. return $this;
  98. }
  99. /**
  100. * Gets the name of a revision metadata field.
  101. *
  102. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  103. * A content entity type definition.
  104. * @param string $key
  105. * The revision metadata key to get, must be one of 'revision_created',
  106. * 'revision_user' or 'revision_log_message'.
  107. *
  108. * @return string
  109. * The name of the field for the specified $key.
  110. */
  111. protected static function getRevisionMetadataKey(EntityTypeInterface $entity_type, $key) {
  112. // We need to prevent ContentEntityType::getRevisionMetadataKey() from
  113. // providing fallback as that requires fetching the entity type's field
  114. // definition leading to an infinite recursion.
  115. /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
  116. $revision_metadata_keys = $entity_type->getRevisionMetadataKeys(FALSE) + [
  117. 'revision_created' => 'revision_created',
  118. 'revision_user' => 'revision_user',
  119. 'revision_log_message' => 'revision_log_message',
  120. ];
  121. return $revision_metadata_keys[$key];
  122. }
  123. }