function EntityOperations::entityPresave

Same name in this branch
  1. 10 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()
  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. 11.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityPresave()
  6. 11.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()
  7. 11.x core/modules/workspaces/src/Hook/EntityOperations.php \Drupal\workspaces\Hook\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.