function ModerationStateFieldItemList::updateModeratedEntity

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

Updates the default revision flag and the publishing status of the entity.

Parameters

string $moderation_state_id: The ID of the new moderation state.

2 calls to ModerationStateFieldItemList::updateModeratedEntity()
ModerationStateFieldItemList::onChange in core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
React to changes to a child property or item.
ModerationStateFieldItemList::setValue in core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
Overrides \Drupal\Core\TypedData\TypedData::setValue().

File

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

Class

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

Namespace

Drupal\content_moderation\Plugin\Field

Code

protected function updateModeratedEntity($moderation_state_id) {
    $entity = $this->getEntity();
    
    /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
    $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
    $workflow = $content_moderation_info->getWorkflowForEntity($entity);
    // Change the entity's default revision flag and the publishing status only
    // if the new workflow state is a valid one.
    if ($workflow && $workflow->getTypePlugin()
        ->hasState($moderation_state_id)) {
        
        /** @var \Drupal\content_moderation\ContentModerationState $current_state */
        $current_state = $workflow->getTypePlugin()
            ->getState($moderation_state_id);
        // This entity is default if it is new, the default revision state, or the
        // default revision is not published.
        if (!$entity->isSyncing()) {
            $update_default_revision = $entity->isNew() || $current_state->isDefaultRevisionState() || !$content_moderation_info->isDefaultRevisionPublished($entity);
            $entity->isDefaultRevision($update_default_revision);
        }
        // Update publishing status if it can be updated and if it needs updating.
        $published_state = $current_state->isPublishedState();
        if ($entity instanceof EntityPublishedInterface && $entity->isPublished() !== $published_state) {
            $published_state ? $entity->setPublished() : $entity->setUnpublished();
        }
    }
}

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