Same name in this branch
  1. 10 core/modules/content_moderation/src/ContentModerationState.php \Drupal\content_moderation\ContentModerationState
  2. 10 core/modules/content_moderation/src/Entity/ContentModerationState.php \Drupal\content_moderation\Entity\ContentModerationState
Same name and namespace in other branches
  1. 8.9.x core/modules/content_moderation/src/Entity/ContentModerationState.php \Drupal\content_moderation\Entity\ContentModerationState
  2. 9 core/modules/content_moderation/src/Entity/ContentModerationState.php \Drupal\content_moderation\Entity\ContentModerationState

Defines the Content moderation state entity.

@ContentEntityType( id = "content_moderation_state", label = @Translation("Content moderation state"), label_singular = @Translation("content moderation state"), label_plural = @Translation("content moderation states"), label_count = @PluralTranslation( singular = "@count content moderation state", plural = "@count content moderation states" ), handlers = { "storage_schema" = "Drupal\content_moderation\ContentModerationStateStorageSchema", "views_data" = "\Drupal\views\EntityViewsData", "access" = "Drupal\content_moderation\ContentModerationStateAccessControlHandler", }, base_table = "content_moderation_state", revision_table = "content_moderation_state_revision", data_table = "content_moderation_state_field_data", revision_data_table = "content_moderation_state_field_revision", translatable = TRUE, internal = TRUE, entity_keys = { "id" = "id", "revision" = "revision_id", "uuid" = "uuid", "uid" = "uid", "owner" = "uid", "langcode" = "langcode", } )

@internal This entity is marked internal because it should not be used directly to alter the moderation state of an entity. Instead, the computed moderation_state field should be set on the entity directly.

Hierarchy

Expanded class hierarchy of ContentModerationState

4 files declare their use of ContentModerationState
ContentModerationResaveTest.php in core/modules/content_moderation/tests/src/Kernel/ContentModerationResaveTest.php
ContentModerationStateAccessControlHandlerTest.php in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateAccessControlHandlerTest.php
ContentModerationStateStorageSchemaTest.php in core/modules/content_moderation/tests/src/Kernel/ContentModerationStateStorageSchemaTest.php
EntityOperations.php in core/modules/content_moderation/src/EntityOperations.php

File

core/modules/content_moderation/src/Entity/ContentModerationState.php, line 50

Namespace

Drupal\content_moderation\Entity
View source
class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields += static::ownerBaseFieldDefinitions($entity_type);
    $fields['uid']
      ->setLabel(t('User'))
      ->setDescription(t('The username of the entity creator.'))
      ->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)
      ->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;
  }

  /**
   * 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();
  }

  /**
   * Loads a content moderation state entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   A moderated entity object.
   *
   * @return \Drupal\content_moderation\Entity\ContentModerationStateInterface|null
   *   The related content moderation state or NULL if none could be found.
   *
   * @internal
   *   This method should only be called by code directly handling the
   *   ContentModerationState entity objects.
   */
  public static function loadFromModeratedEntity(EntityInterface $entity) {
    $content_moderation_state = NULL;
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    if ($moderation_info
      ->isModeratedEntity($entity)) {

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $storage = \Drupal::entityTypeManager()
        ->getStorage('content_moderation_state');

      // New entities may not have a loaded revision ID at this point, but the
      // creation of a content moderation state entity may have already been
      // triggered elsewhere. In this case we have to match on the revision ID
      // (instead of the loaded revision ID).
      $revision_id = $entity
        ->getLoadedRevisionId() ?: $entity
        ->getRevisionId();
      $ids = $storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('content_entity_type_id', $entity
        ->getEntityTypeId())
        ->condition('content_entity_id', $entity
        ->id())
        ->condition('workflow', $moderation_info
        ->getWorkflowForEntity($entity)
        ->id())
        ->condition('content_entity_revision_id', $revision_id)
        ->allRevisions()
        ->execute();
      if ($ids) {

        /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
        $content_moderation_state = $storage
          ->loadRevision(key($ids));
      }
    }
    return $content_moderation_state;
  }

  /**
   * {@inheritdoc}
   */
  public function save() {

    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()
      ->getStorage($this->content_entity_type_id->value);
    $related_entity = $storage
      ->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();
  }

  /**
   * {@inheritdoc}
   */
  protected function getFieldsToSkipFromTranslationChangesCheck() {
    $field_names = parent::getFieldsToSkipFromTranslationChangesCheck();

    // We need to skip the parent entity revision ID, since that will always
    // change on every save, otherwise every translation would be marked as
    // affected regardless of actual changes.
    $field_names[] = 'content_entity_revision_id';
    return $field_names;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessibleInterface::access public function Checks data value access. 6
CacheableDependencyInterface::getCacheContexts public function The cache contexts associated with this object. 12
CacheableDependencyInterface::getCacheMaxAge public function The maximum age for which this object may be cached. 12
CacheableDependencyInterface::getCacheTags public function The cache tags associated with this object. 12
ContentModerationState::baseFieldDefinitions public static function Provides base field definitions for an entity type. Overrides FieldableEntityInterface::baseFieldDefinitions
ContentModerationState::getFieldsToSkipFromTranslationChangesCheck protected function
ContentModerationState::loadFromModeratedEntity public static function Loads a content moderation state entity.
ContentModerationState::realSave protected function Saves an entity permanently.
ContentModerationState::save public function Saves an entity permanently. Overrides EntityInterface::save
ContentModerationState::updateOrCreateFromEntity public static function Creates or updates an entity's moderation state whilst saving that entity.
EntityInterface::bundle public function Gets the bundle of the entity. 1
EntityInterface::create public static function Constructs a new entity object, without permanently saving it. 1
EntityInterface::createDuplicate public function Creates a duplicate of the entity. 2
EntityInterface::delete public function Deletes an entity permanently. 1
EntityInterface::enforceIsNew public function Enforces an entity to be new. 1
EntityInterface::getCacheTagsToInvalidate public function Returns the cache tags that should be used to invalidate caches. 3
EntityInterface::getConfigDependencyKey public function Gets the key that is used to store configuration dependencies. 1
EntityInterface::getConfigDependencyName public function Gets the configuration dependency name. 1
EntityInterface::getConfigTarget public function Gets the configuration target identifier for the entity. 1
EntityInterface::getEntityType public function Gets the entity type definition. 1
EntityInterface::getEntityTypeId public function Gets the ID of the type of the entity. 1
EntityInterface::getOriginalId public function Gets the original ID. 1
EntityInterface::getTypedData public function Gets a typed data object for this entity object. 1
EntityInterface::hasLinkTemplate public function Indicates if a link template exists for a given key. 1
EntityInterface::id public function Gets the identifier. 1
EntityInterface::isNew public function Determines whether the entity is new. 1
EntityInterface::label public function Gets the label of the entity. 3
EntityInterface::language public function Gets the language of the entity. 1
EntityInterface::load public static function Loads an entity. 1
EntityInterface::loadMultiple public static function Loads one or more entities. 1
EntityInterface::postCreate public function Acts on a created entity before hooks are invoked. 1
EntityInterface::postDelete public static function Acts on deleted entities before the delete hook is invoked. 7
EntityInterface::postLoad public static function Acts on loaded entities. 2
EntityInterface::postSave public function Acts on a saved entity before the insert or update hook is invoked. 8
EntityInterface::preCreate public static function Changes the values of an entity before it is created. 4
EntityInterface::preDelete public static function Acts on entities before they are deleted and before hooks are invoked. 6
EntityInterface::preSave public function Acts on an entity before the presave hook is invoked. 8
EntityInterface::referencedEntities public function Gets a list of entities referenced by this entity. 2
EntityInterface::setOriginalId public function Sets the original ID. 1
EntityInterface::toLink public function Generates the HTML for a link to this entity. 1
EntityInterface::toUrl public function Gets the URL object for the entity. 1
EntityInterface::uriRelationships public function Gets a list of URI relationships supported by this entity. 1
EntityInterface::uuid public function Gets the entity UUID (Universally Unique Identifier). 1
EntityOwnerTrait::getDefaultEntityOwner public static function Default value callback for 'owner' base field. 1
EntityOwnerTrait::getOwner public function 1
EntityOwnerTrait::getOwnerId public function
EntityOwnerTrait::ownerBaseFieldDefinitions public static function Returns an array of base field definitions for entity owners.
EntityOwnerTrait::setOwner public function
EntityOwnerTrait::setOwnerId public function
FieldableEntityInterface::bundleFieldDefinitions public static function Provides field definitions for a specific bundle. 2
FieldableEntityInterface::get public function Gets a field item list.
FieldableEntityInterface::getFieldDefinition public function Gets the definition of a contained field.
FieldableEntityInterface::getFieldDefinitions public function Gets an array of field definitions of all contained fields.
FieldableEntityInterface::getFields public function Gets an array of all field item lists.
FieldableEntityInterface::getTranslatableFields public function Gets an array of field item lists for translatable fields.
FieldableEntityInterface::hasField public function Determines whether the entity has a field with the given name.
FieldableEntityInterface::isValidationRequired public function Checks whether entity validation is required before saving the entity.
FieldableEntityInterface::onChange public function Reacts to changes to a field.
FieldableEntityInterface::set public function Sets a field value.
FieldableEntityInterface::setValidationRequired public function Sets whether entity validation is required before saving the entity.
FieldableEntityInterface::toArray public function Gets an array of all field values. Overrides EntityInterface::toArray
FieldableEntityInterface::validate public function Validates the currently set values. 1
RefinableCacheableDependencyInterface::addCacheableDependency public function Adds a dependency on an object: merges its cacheability metadata.
RefinableCacheableDependencyInterface::addCacheContexts public function Adds cache contexts.
RefinableCacheableDependencyInterface::addCacheTags public function Adds cache tags.
RefinableCacheableDependencyInterface::mergeCacheMaxAge public function Merges the maximum age (in seconds) with the existing maximum age.
RevisionableInterface::getLoadedRevisionId public function Gets the loaded Revision ID of the entity.
RevisionableInterface::getRevisionId public function Gets the revision identifier of the entity.
RevisionableInterface::isDefaultRevision public function Checks if this entity is the default revision.
RevisionableInterface::isLatestRevision public function Checks if this entity is the latest revision.
RevisionableInterface::isNewRevision public function Determines whether a new revision should be created on save.
RevisionableInterface::preSaveRevision public function Acts on a revision before it gets saved. 3
RevisionableInterface::setNewRevision public function Enforces an entity to be saved as a new revision.
RevisionableInterface::updateLoadedRevisionId public function Updates the loaded Revision ID with the revision ID.
RevisionableInterface::wasDefaultRevision public function Checks whether the entity object was a default revision when it was saved.
SynchronizableInterface::isSyncing public function Returns whether this entity is being changed as part of a synchronization.
SynchronizableInterface::setSyncing public function Sets the status of the synchronization flag.
TranslatableInterface::addTranslation public function Adds a new translation to the translatable object.
TranslatableInterface::getTranslation public function Gets a translation of the data.
TranslatableInterface::getTranslationLanguages public function Returns the languages the data is translated to.
TranslatableInterface::getUntranslated public function Returns the translatable object in the language it was created.
TranslatableInterface::hasTranslation public function Checks there is a translation for the given language code.
TranslatableInterface::hasTranslationChanges public function Determines if the current translation of the entity has unsaved changes.
TranslatableInterface::isDefaultTranslation public function Checks whether the translation is the default one.
TranslatableInterface::isNewTranslation public function Checks whether the translation is new.
TranslatableInterface::isTranslatable public function Returns the translation support status.
TranslatableInterface::removeTranslation public function Removes the translation identified by the given language code.
TranslatableRevisionableInterface::isDefaultTranslationAffectedOnly public function Checks if untranslatable fields should affect only the default translation.
TranslatableRevisionableInterface::isLatestTranslationAffectedRevision public function Checks whether this is the latest revision affecting this translation.
TranslatableRevisionableInterface::isRevisionTranslationAffected public function Checks whether the current translation is affected by the current revision.
TranslatableRevisionableInterface::isRevisionTranslationAffectedEnforced public function Checks if the revision translation affected flag value has been enforced.
TranslatableRevisionableInterface::setRevisionTranslationAffected public function Marks the current revision translation as affected.
TranslatableRevisionableInterface::setRevisionTranslationAffectedEnforced public function Enforces the revision translation affected flag value.