e54fbbe2a8203e5d835815eea94997dfcaa987bc
[yaffs-website] / web / core / modules / content_moderation / src / Entity / ContentModerationState.php
1 <?php
2
3 namespace Drupal\content_moderation\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\Core\TypedData\TranslatableInterface;
10 use Drupal\user\UserInterface;
11
12 /**
13  * Defines the Content moderation state entity.
14  *
15  * @ContentEntityType(
16  *   id = "content_moderation_state",
17  *   label = @Translation("Content moderation state"),
18  *   label_singular = @Translation("content moderation state"),
19  *   label_plural = @Translation("content moderation states"),
20  *   label_count = @PluralTranslation(
21  *     singular = "@count content moderation state",
22  *     plural = "@count content moderation states"
23  *   ),
24  *   handlers = {
25  *     "storage_schema" = "Drupal\content_moderation\ContentModerationStateStorageSchema",
26  *     "views_data" = "\Drupal\views\EntityViewsData",
27  *     "access" = "Drupal\content_moderation\ContentModerationStateAccessControlHandler",
28  *   },
29  *   base_table = "content_moderation_state",
30  *   revision_table = "content_moderation_state_revision",
31  *   data_table = "content_moderation_state_field_data",
32  *   revision_data_table = "content_moderation_state_field_revision",
33  *   translatable = TRUE,
34  *   entity_keys = {
35  *     "id" = "id",
36  *     "revision" = "revision_id",
37  *     "uuid" = "uuid",
38  *     "uid" = "uid",
39  *     "langcode" = "langcode",
40  *   }
41  * )
42  *
43  * @internal
44  *   This entity is marked internal because it should not be used directly to
45  *   alter the moderation state of an entity. Instead, the computed
46  *   moderation_state field should be set on the entity directly.
47  */
48 class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
54     $fields = parent::baseFieldDefinitions($entity_type);
55
56     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
57       ->setLabel(t('User'))
58       ->setDescription(t('The username of the entity creator.'))
59       ->setSetting('target_type', 'user')
60       ->setDefaultValueCallback('Drupal\content_moderation\Entity\ContentModerationState::getCurrentUserId')
61       ->setTranslatable(TRUE)
62       ->setRevisionable(TRUE);
63
64     $fields['workflow'] = BaseFieldDefinition::create('entity_reference')
65       ->setLabel(t('Workflow'))
66       ->setDescription(t('The workflow the moderation state is in.'))
67       ->setSetting('target_type', 'workflow')
68       ->setRequired(TRUE)
69       ->setRevisionable(TRUE);
70
71     $fields['moderation_state'] = BaseFieldDefinition::create('string')
72       ->setLabel(t('Moderation state'))
73       ->setDescription(t('The moderation state of the referenced content.'))
74       ->setRequired(TRUE)
75       ->setTranslatable(TRUE)
76       ->setRevisionable(TRUE);
77
78     $fields['content_entity_type_id'] = BaseFieldDefinition::create('string')
79       ->setLabel(t('Content entity type ID'))
80       ->setDescription(t('The ID of the content entity type this moderation state is for.'))
81       ->setRequired(TRUE)
82       ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH)
83       ->setRevisionable(TRUE);
84
85     $fields['content_entity_id'] = BaseFieldDefinition::create('integer')
86       ->setLabel(t('Content entity ID'))
87       ->setDescription(t('The ID of the content entity this moderation state is for.'))
88       ->setRequired(TRUE)
89       ->setRevisionable(TRUE);
90
91     $fields['content_entity_revision_id'] = BaseFieldDefinition::create('integer')
92       ->setLabel(t('Content entity revision ID'))
93       ->setDescription(t('The revision ID of the content entity this moderation state is for.'))
94       ->setRequired(TRUE)
95       ->setRevisionable(TRUE);
96
97     return $fields;
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getOwner() {
104     return $this->get('uid')->entity;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function getOwnerId() {
111     return $this->getEntityKey('uid');
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function setOwnerId($uid) {
118     $this->set('uid', $uid);
119     return $this;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function setOwner(UserInterface $account) {
126     $this->set('uid', $account->id());
127     return $this;
128   }
129
130   /**
131    * Creates or updates an entity's moderation state whilst saving that entity.
132    *
133    * @param \Drupal\content_moderation\Entity\ContentModerationState $content_moderation_state
134    *   The content moderation entity content entity to create or save.
135    *
136    * @internal
137    *   This method should only be called as a result of saving the related
138    *   content entity.
139    */
140   public static function updateOrCreateFromEntity(ContentModerationState $content_moderation_state) {
141     $content_moderation_state->realSave();
142   }
143
144   /**
145    * Loads a content moderation state entity.
146    *
147    * @param \Drupal\Core\Entity\EntityInterface $entity
148    *   A moderated entity object.
149    *
150    * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
151    *   The related content moderation state or NULL if none could be found.
152    *
153    * @internal
154    *   This method should only be called by code directly handling the
155    *   ContentModerationState entity objects.
156    */
157   public static function loadFromModeratedEntity(EntityInterface $entity) {
158     $content_moderation_state = NULL;
159     $moderation_info = \Drupal::service('content_moderation.moderation_information');
160
161     if ($moderation_info->isModeratedEntity($entity)) {
162       /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
163       $storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
164
165       $ids = $storage->getQuery()
166         ->condition('content_entity_type_id', $entity->getEntityTypeId())
167         ->condition('content_entity_id', $entity->id())
168         ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id())
169         ->condition('content_entity_revision_id', $entity->getLoadedRevisionId())
170         ->allRevisions()
171         ->execute();
172
173       if ($ids) {
174         /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
175         $content_moderation_state = $storage->loadRevision(key($ids));
176       }
177     }
178
179     return $content_moderation_state;
180   }
181
182   /**
183    * Default value callback for the 'uid' base field definition.
184    *
185    * @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions()
186    *
187    * @return array
188    *   An array of default values.
189    */
190   public static function getCurrentUserId() {
191     return [\Drupal::currentUser()->id()];
192   }
193
194   /**
195    * {@inheritdoc}
196    */
197   public function save() {
198     $related_entity = \Drupal::entityTypeManager()
199       ->getStorage($this->content_entity_type_id->value)
200       ->loadRevision($this->content_entity_revision_id->value);
201     if ($related_entity instanceof TranslatableInterface) {
202       $related_entity = $related_entity->getTranslation($this->activeLangcode);
203     }
204     $related_entity->moderation_state = $this->moderation_state;
205     return $related_entity->save();
206   }
207
208   /**
209    * Saves an entity permanently.
210    *
211    * When saving existing entities, the entity is assumed to be complete,
212    * partial updates of entities are not supported.
213    *
214    * @return int
215    *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
216    *
217    * @throws \Drupal\Core\Entity\EntityStorageException
218    *   In case of failures an exception is thrown.
219    */
220   protected function realSave() {
221     return parent::save();
222   }
223
224 }