function EntityOperations::entityPresave

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

Implements hook_entity_presave().

Attributes

#[Hook('entity_presave', order: Order::First)] #[ReorderHook('entity_presave', class: ContentModerationHooks::class, method: 'entityPresave', order: new OrderBefore([ 'workspaces', ]))]

File

core/modules/workspaces/src/Hook/EntityOperations.php, line 79

Class

EntityOperations
Defines a class for reacting to entity runtime hooks.

Namespace

Drupal\workspaces\Hook

Code

public function entityPresave(EntityInterface $entity) : void {
  if ($this->shouldSkipOperations($entity)) {
    return;
  }
  // Disallow any change to an unsupported entity when we are not in the
  // default workspace.
  if (!$this->workspaceInfo
    ->isEntitySupported($entity)) {
    throw new \RuntimeException(sprintf('The "%s" entity type can only be saved in the default workspace.', $entity->getEntityTypeId()));
  }
  /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
  if (!$entity->isNew() && !$entity->isSyncing()) {
    // Force a new revision if the entity is not replicating.
    $entity->setNewRevision(TRUE);
    // All entities in the non-default workspace are pending revisions,
    // regardless of their publishing status. This means that when creating
    // a published pending revision in a non-default workspace it will also be
    // a published pending revision in the default workspace, however, it will
    // become the default revision only when it is replicated to the default
    // workspace.
    $entity->isDefaultRevision(FALSE);
  }
  // In ::entityFormEntityBuild() we mark the entity as a non-default revision
  // so that validation constraints can rely on $entity->isDefaultRevision()
  // always returning FALSE when an entity form is submitted in a workspace.
  // However, after validation has run, we need to revert that flag so the
  // first revision of a new entity is correctly seen by the system as the
  // default revision.
  if ($entity->isNew()) {
    $entity->isDefaultRevision(TRUE);
  }
  // Track the workspaces in which the new revision was saved.
  if (!$entity->isSyncing()) {
    $field_name = $entity->getEntityType()
      ->getRevisionMetadataKey('workspace');
    $entity->{$field_name}->target_id = $this->workspaceManager
      ->getActiveWorkspace()
      ->id();
  }
  // When a new published entity is inserted in a non-default workspace, we
  // actually want two revisions to be saved:
  // - An unpublished default revision in the default ('live') workspace.
  // - A published pending revision in the current workspace.
  if ($entity->isNew() && $entity->isPublished()) {
    // Keep track of the initially published entities for ::entityInsert(),
    // then unpublish the default revision.
    $this->initialPublished[$entity->uuid()] = TRUE;
    $entity->setUnpublished();
  }
}

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