You are here

public function EntityOperations::entityPresave in Drupal 8

Same name in this branch
  1. 8 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityPresave()
  2. 8 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()
Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\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());
  }
}