function WorkspaceProviderBase::entityInsert
Acts after an entity has been added.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity that was inserted.
Overrides WorkspaceProviderInterface::entityInsert
File
-
core/
modules/ workspaces/ src/ Provider/ WorkspaceProviderBase.php, line 171
Class
- WorkspaceProviderBase
- Defines the base class for workspace providers.
Namespace
Drupal\workspaces\ProviderCode
public function entityInsert(EntityInterface $entity) : void {
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($this->initialPublished[$entity->uuid()])) {
// 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();
// Ensure that the second (workspace-specific) revision is marked as new
// early, so operations that are executed before the entity presave hook
// (e.g. field-level presave) can take that into account.
$entity->setNewRevision();
$entity->isDefaultRevision(FALSE);
$entity->save();
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.