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/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityPresave()

Acts on an entity before it is created or updated.

Parameters

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

See also

hook_entity_presave()

File

core/modules/workspaces/src/EntityOperations.php, line 111

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspaces

Code

public function entityPresave(EntityInterface $entity) {
  $entity_type = $entity
    ->getEntityType();

  // Only run if we are not dealing with an entity type provided by the
  // Workspaces module, an internal entity type or if we are in a non-default
  // workspace.
  if ($this
    ->shouldSkipPreOperations($entity_type)) {
    return;
  }

  // Disallow any change to an unsupported entity when we are not in the
  // default workspace.
  if (!$this->workspaceManager
    ->isEntityTypeSupported($entity_type)) {
    throw new \RuntimeException('This entity can only be saved in the default workspace.');
  }

  /** @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);

    // Track the workspaces in which the new revision was saved.
    $field_name = $entity_type
      ->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 publishing status in a dynamic property for
    // ::entityInsert(), then unpublish the default revision.
    // @todo Remove this dynamic property once we have an API for associating
    //   temporary data with an entity: https://www.drupal.org/node/2896474.
    $entity->_initialPublished = TRUE;
    $entity
      ->setUnpublished();
  }
}