function EntityOperations::entityPresave

Same name in this branch
  1. 11.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()
Same name in other branches
  1. 9 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityPresave()
  2. 9 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()
  3. 8.9.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityPresave()
  4. 8.9.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()
  5. 10 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityPresave()
  6. 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()

Acts on an entity and set published status based on the moderation state.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity being saved.

See also

hook_entity_presave()

File

core/modules/content_moderation/src/EntityOperations.php, line 105

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\content_moderation

Code

public function entityPresave(EntityInterface $entity) {
    if (!$this->moderationInfo
        ->isModeratedEntity($entity)) {
        return;
    }
    if ($entity->moderation_state->value) {
        $workflow = $this->moderationInfo
            ->getWorkflowForEntity($entity);
        
        /** @var \Drupal\content_moderation\ContentModerationState $current_state */
        $current_state = $workflow->getTypePlugin()
            ->getState($entity->moderation_state->value);
        // This entity is default if it is new, the default revision, or the
        // default revision is not published.
        $update_default_revision = $entity->isNew() || $current_state->isDefaultRevisionState() || !$this->moderationInfo
            ->isDefaultRevisionPublished($entity);
        // Fire per-entity-type logic for handling the save process.
        $this->entityTypeManager
            ->getHandler($entity->getEntityTypeId(), 'moderation')
            ->onPresave($entity, $update_default_revision, $current_state->isPublishedState());
    }
}

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