You are here

public function EntityOperations::entityPresave in Workspace 8.2

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

src/EntityOperations.php, line 121

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspace

Code

public function entityPresave(EntityInterface $entity) {

  /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */

  // Only run if the entity type can belong to a workspace and we are in a
  // non-default workspace.
  if (!$this->workspaceManager
    ->shouldAlterOperations($entity
    ->getEntityType())) {
    return;
  }
  if (!$entity
    ->isNew() && !isset($entity->_isReplicating)) {

    // 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);
  }

  // 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 for workspace_entity_insert() and
    // 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();
  }
}