3 namespace Drupal\content_moderation\Entity;
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;
13 * Defines the Content moderation state entity.
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"
25 * "storage_schema" = "Drupal\content_moderation\ContentModerationStateStorageSchema",
26 * "views_data" = "\Drupal\views\EntityViewsData",
27 * "access" = "Drupal\content_moderation\ContentModerationStateAccessControlHandler",
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,
36 * "revision" = "revision_id",
39 * "langcode" = "langcode",
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.
48 class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
53 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
54 $fields = parent::baseFieldDefinitions($entity_type);
56 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
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);
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')
69 ->setRevisionable(TRUE);
71 $fields['moderation_state'] = BaseFieldDefinition::create('string')
72 ->setLabel(t('Moderation state'))
73 ->setDescription(t('The moderation state of the referenced content.'))
75 ->setTranslatable(TRUE)
76 ->setRevisionable(TRUE);
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.'))
82 ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH)
83 ->setRevisionable(TRUE);
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.'))
89 ->setRevisionable(TRUE);
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.'))
95 ->setRevisionable(TRUE);
103 public function getOwner() {
104 return $this->get('uid')->entity;
110 public function getOwnerId() {
111 return $this->getEntityKey('uid');
117 public function setOwnerId($uid) {
118 $this->set('uid', $uid);
125 public function setOwner(UserInterface $account) {
126 $this->set('uid', $account->id());
131 * Creates or updates an entity's moderation state whilst saving that entity.
133 * @param \Drupal\content_moderation\Entity\ContentModerationState $content_moderation_state
134 * The content moderation entity content entity to create or save.
137 * This method should only be called as a result of saving the related
140 public static function updateOrCreateFromEntity(ContentModerationState $content_moderation_state) {
141 $content_moderation_state->realSave();
145 * Loads a content moderation state entity.
147 * @param \Drupal\Core\Entity\EntityInterface $entity
148 * A moderated entity object.
150 * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
151 * The related content moderation state or NULL if none could be found.
154 * This method should only be called by code directly handling the
155 * ContentModerationState entity objects.
157 public static function loadFromModeratedEntity(EntityInterface $entity) {
158 $content_moderation_state = NULL;
159 $moderation_info = \Drupal::service('content_moderation.moderation_information');
161 if ($moderation_info->isModeratedEntity($entity)) {
162 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
163 $storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
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())
174 /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
175 $content_moderation_state = $storage->loadRevision(key($ids));
179 return $content_moderation_state;
183 * Default value callback for the 'uid' base field definition.
185 * @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions()
188 * An array of default values.
190 public static function getCurrentUserId() {
191 return [\Drupal::currentUser()->id()];
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);
204 $related_entity->moderation_state = $this->moderation_state;
205 return $related_entity->save();
209 * Saves an entity permanently.
211 * When saving existing entities, the entity is assumed to be complete,
212 * partial updates of entities are not supported.
215 * Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
217 * @throws \Drupal\Core\Entity\EntityStorageException
218 * In case of failures an exception is thrown.
220 protected function realSave() {
221 return parent::save();