You are here

public function EntityOperations::entityInsert in Workspace 8.2

Responds to the creation of a new entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity that was just saved.

See also

hook_entity_insert()

File

src/EntityOperations.php, line 164

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspace

Code

public function entityInsert(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;
  }
  $this
    ->trackEntity($entity);

  // Handle the case when a new published entity was created in a non-default
  // workspace and create a published pending revision for it. This does not
  // cause an infinite recursion with ::entityPresave() because at this point
  // the entity is no longer new.
  // @todo Better explain in https://www.drupal.org/node/2962764
  if (isset($entity->_initialPublished)) {

    // Operate on a clone to avoid changing the entity prior to subsequent
    // hook_entity_insert() implementations.
    $pending_revision = clone $entity;
    $pending_revision
      ->setPublished();
    $pending_revision
      ->isDefaultRevision(FALSE);
    $pending_revision
      ->save();
  }
}