3 namespace Drupal\content_moderation\Entity;
5 use Drupal\content_moderation\ContentModerationStateInterface;
6 use Drupal\Core\Entity\ContentEntityBase;
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",
43 class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
48 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
49 $fields = parent::baseFieldDefinitions($entity_type);
51 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
53 ->setDescription(t('The username of the entity creator.'))
54 ->setSetting('target_type', 'user')
55 ->setDefaultValueCallback('Drupal\content_moderation\Entity\ContentModerationState::getCurrentUserId')
56 ->setTranslatable(TRUE)
57 ->setRevisionable(TRUE);
59 $fields['workflow'] = BaseFieldDefinition::create('entity_reference')
60 ->setLabel(t('Workflow'))
61 ->setDescription(t('The workflow the moderation state is in.'))
62 ->setSetting('target_type', 'workflow')
64 ->setTranslatable(TRUE)
65 ->setRevisionable(TRUE);
67 $fields['moderation_state'] = BaseFieldDefinition::create('string')
68 ->setLabel(t('Moderation state'))
69 ->setDescription(t('The moderation state of the referenced content.'))
71 ->setTranslatable(TRUE)
72 ->setRevisionable(TRUE);
74 $fields['content_entity_type_id'] = BaseFieldDefinition::create('string')
75 ->setLabel(t('Content entity type ID'))
76 ->setDescription(t('The ID of the content entity type this moderation state is for.'))
78 ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH)
79 ->setRevisionable(TRUE);
81 $fields['content_entity_id'] = BaseFieldDefinition::create('integer')
82 ->setLabel(t('Content entity ID'))
83 ->setDescription(t('The ID of the content entity this moderation state is for.'))
85 ->setRevisionable(TRUE);
87 $fields['content_entity_revision_id'] = BaseFieldDefinition::create('integer')
88 ->setLabel(t('Content entity revision ID'))
89 ->setDescription(t('The revision ID of the content entity this moderation state is for.'))
91 ->setRevisionable(TRUE);
99 public function getOwner() {
100 return $this->get('uid')->entity;
106 public function getOwnerId() {
107 return $this->getEntityKey('uid');
113 public function setOwnerId($uid) {
114 $this->set('uid', $uid);
121 public function setOwner(UserInterface $account) {
122 $this->set('uid', $account->id());
127 * Creates or updates an entity's moderation state whilst saving that entity.
129 * @param \Drupal\content_moderation\Entity\ContentModerationState $content_moderation_state
130 * The content moderation entity content entity to create or save.
133 * This method should only be called as a result of saving the related
136 public static function updateOrCreateFromEntity(ContentModerationState $content_moderation_state) {
137 $content_moderation_state->realSave();
141 * Default value callback for the 'uid' base field definition.
143 * @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions()
146 * An array of default values.
148 public static function getCurrentUserId() {
149 return [\Drupal::currentUser()->id()];
155 public function save() {
156 $related_entity = \Drupal::entityTypeManager()
157 ->getStorage($this->content_entity_type_id->value)
158 ->loadRevision($this->content_entity_revision_id->value);
159 if ($related_entity instanceof TranslatableInterface) {
160 $related_entity = $related_entity->getTranslation($this->activeLangcode);
162 $related_entity->moderation_state = $this->moderation_state;
163 return $related_entity->save();
167 * Saves an entity permanently.
169 * When saving existing entities, the entity is assumed to be complete,
170 * partial updates of entities are not supported.
173 * Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
175 * @throws \Drupal\Core\Entity\EntityStorageException
176 * In case of failures an exception is thrown.
178 protected function realSave() {
179 return parent::save();