function EntityOperations::entityInsert

Same name in this branch
  1. 8.9.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\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. 10 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityInsert()
  4. 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityInsert()
  5. 11.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::entityInsert()
  6. 11.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityInsert()

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

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

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspaces

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->workspaceAssociation
        ->trackEntity($entity, $this->workspaceManager
        ->getActiveWorkspace());
    // When an entity is newly created in a workspace, it should be published in
    // that workspace, but not yet published on 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 published 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)) {
        // 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();
    }
}

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