function EntityOperations::entityInsert

Same name in this branch
  1. 11.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityInsert()
  2. 11.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityInsert()
Same name in other branches
  1. 9 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityInsert()
  2. 9 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityInsert()
  3. 8.9.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityInsert()
  4. 8.9.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityInsert()
  5. 10 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityInsert()
  6. 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityInsert()

Implements hook_entity_insert().

File

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

Class

EntityOperations
Defines a class for reacting to entity runtime hooks.

Namespace

Drupal\workspaces\Hook

Code

public function entityInsert(EntityInterface $entity) : void {
    if ($entity->getEntityTypeId() === 'workspace') {
        $this->workspaceAssociation
            ->workspaceInsert($entity);
        $this->workspaceRepository
            ->resetCache();
    }
    if ($this->shouldSkipOperations($entity) || !$this->workspaceInfo
        ->isEntitySupported($entity)) {
        return;
    }
    assert($entity instanceof RevisionableInterface && $entity instanceof EntityPublishedInterface);
    $this->workspaceAssociation
        ->trackEntity($entity, $this->workspaceManager
        ->getActiveWorkspace());
    // When a published entity is created in a workspace, it should remain
    // published only in that workspace, and unpublished in the live workspace.
    // It is first saved as unpublished for the default revision, then
    // immediately a second revision is created which is published and attached
    // to the workspace. This ensures that the initial version of the entity
    // does not 'leak' into the live site. This differs from edits to existing
    // entities where there is already a valid default revision for the live
    // workspace.
    if (isset($entity->_initialPublished)) {
        // Ensure that the default revision of an entity saved in a workspace is
        // unpublished.
        if ($entity->isPublished()) {
            throw new \RuntimeException('The default revision of an entity created in a workspace cannot be published.');
        }
        $entity->setPublished();
        $entity->isDefaultRevision(FALSE);
        $entity->save();
    }
}

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