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