function ModerationStateFieldItemList::loadContentModerationStateRevision

Same name in other branches
  1. 9 core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList::loadContentModerationStateRevision()
  2. 10 core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList::loadContentModerationStateRevision()
  3. 11.x core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList::loadContentModerationStateRevision()

Load the content moderation state revision associated with an entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity the content moderation state entity will be loaded from.

Return value

\Drupal\content_moderation\Entity\ContentModerationStateInterface|null The content_moderation_state revision or FALSE if none exists.

1 call to ModerationStateFieldItemList::loadContentModerationStateRevision()
ModerationStateFieldItemList::getModerationStateId in core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
Gets the moderation state ID linked to a content entity revision.

File

core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php, line 73

Class

ModerationStateFieldItemList
A computed field that provides a content entity's moderation state.

Namespace

Drupal\content_moderation\Plugin\Field

Code

protected function loadContentModerationStateRevision(ContentEntityInterface $entity) {
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    $content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');
    $revisions = $content_moderation_storage->getQuery()
        ->condition('content_entity_type_id', $entity->getEntityTypeId())
        ->condition('content_entity_id', $entity->id())
        ->condition('content_entity_revision_id', $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId())
        ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)
        ->id())
        ->condition('langcode', $entity->language()
        ->getId())
        ->allRevisions()
        ->sort('revision_id', 'DESC')
        ->execute();
    if (empty($revisions)) {
        return NULL;
    }
    
    /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
    $content_moderation_state = $content_moderation_storage->loadRevision(key($revisions));
    if ($entity->getEntityType()
        ->hasKey('langcode')) {
        $langcode = $entity->language()
            ->getId();
        if (!$content_moderation_state->hasTranslation($langcode)) {
            $content_moderation_state->addTranslation($langcode, $content_moderation_state->toArray());
        }
        if ($content_moderation_state->language()
            ->getId() !== $langcode) {
            $content_moderation_state = $content_moderation_state->getTranslation($langcode);
        }
    }
    return $content_moderation_state;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.