setLabel(t('User')) ->setDescription(t('The username of the entity creator.')) ->setSetting('target_type', 'user') ->setDefaultValueCallback('Drupal\content_moderation\Entity\ContentModerationState::getCurrentUserId') ->setTranslatable(TRUE) ->setRevisionable(TRUE); $fields['workflow'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Workflow')) ->setDescription(t('The workflow the moderation state is in.')) ->setSetting('target_type', 'workflow') ->setRequired(TRUE) ->setTranslatable(TRUE) ->setRevisionable(TRUE); $fields['moderation_state'] = BaseFieldDefinition::create('string') ->setLabel(t('Moderation state')) ->setDescription(t('The moderation state of the referenced content.')) ->setRequired(TRUE) ->setTranslatable(TRUE) ->setRevisionable(TRUE); $fields['content_entity_type_id'] = BaseFieldDefinition::create('string') ->setLabel(t('Content entity type ID')) ->setDescription(t('The ID of the content entity type this moderation state is for.')) ->setRequired(TRUE) ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH) ->setRevisionable(TRUE); $fields['content_entity_id'] = BaseFieldDefinition::create('integer') ->setLabel(t('Content entity ID')) ->setDescription(t('The ID of the content entity this moderation state is for.')) ->setRequired(TRUE) ->setRevisionable(TRUE); $fields['content_entity_revision_id'] = BaseFieldDefinition::create('integer') ->setLabel(t('Content entity revision ID')) ->setDescription(t('The revision ID of the content entity this moderation state is for.')) ->setRequired(TRUE) ->setRevisionable(TRUE); return $fields; } /** * {@inheritdoc} */ public function getOwner() { return $this->get('uid')->entity; } /** * {@inheritdoc} */ public function getOwnerId() { return $this->getEntityKey('uid'); } /** * {@inheritdoc} */ public function setOwnerId($uid) { $this->set('uid', $uid); return $this; } /** * {@inheritdoc} */ public function setOwner(UserInterface $account) { $this->set('uid', $account->id()); return $this; } /** * Creates or updates an entity's moderation state whilst saving that entity. * * @param \Drupal\content_moderation\Entity\ContentModerationState $content_moderation_state * The content moderation entity content entity to create or save. * * @internal * This method should only be called as a result of saving the related * content entity. */ public static function updateOrCreateFromEntity(ContentModerationState $content_moderation_state) { $content_moderation_state->realSave(); } /** * Default value callback for the 'uid' base field definition. * * @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions() * * @return array * An array of default values. */ public static function getCurrentUserId() { return [\Drupal::currentUser()->id()]; } /** * {@inheritdoc} */ public function save() { $related_entity = \Drupal::entityTypeManager() ->getStorage($this->content_entity_type_id->value) ->loadRevision($this->content_entity_revision_id->value); if ($related_entity instanceof TranslatableInterface) { $related_entity = $related_entity->getTranslation($this->activeLangcode); } $related_entity->moderation_state = $this->moderation_state; return $related_entity->save(); } /** * Saves an entity permanently. * * When saving existing entities, the entity is assumed to be complete, * partial updates of entities are not supported. * * @return int * Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed. * * @throws \Drupal\Core\Entity\EntityStorageException * In case of failures an exception is thrown. */ protected function realSave() { return parent::save(); } }