protected function EntityOperations::trackEntity in Workspace 8.2
Updates or creates a WorkspaceAssociation entity for a given entity.
If the passed-in entity can belong to a workspace and already has a WorkspaceAssociation entity, then a new revision of this will be created with the new information. Otherwise, a new WorkspaceAssociation entity is created to store the passed-in entity's information.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity to update or create from.
2 calls to EntityOperations::trackEntity()
- EntityOperations::entityInsert in src/
EntityOperations.php - Responds to the creation of a new entity.
- EntityOperations::entityUpdate in src/
EntityOperations.php - Responds to updates to an entity.
File
- src/
EntityOperations.php, line 222
Class
- EntityOperations
- Defines a class for reacting to entity events.
Namespace
Drupal\workspaceCode
protected function trackEntity(EntityInterface $entity) {
/** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
// If the entity is not new, check if there's an existing
// WorkspaceAssociation entity for it.
$workspace_association_storage = $this->entityTypeManager
->getStorage('workspace_association');
if (!$entity
->isNew()) {
$workspace_associations = $workspace_association_storage
->loadByProperties([
'target_entity_type_id' => $entity
->getEntityTypeId(),
'target_entity_id' => $entity
->id(),
]);
/** @var \Drupal\Core\Entity\ContentEntityInterface $workspace_association */
$workspace_association = reset($workspace_associations);
}
// If there was a WorkspaceAssociation entry create a new revision,
// otherwise create a new entity with the type and ID.
if (!empty($workspace_association)) {
$workspace_association
->setNewRevision(TRUE);
}
else {
$workspace_association = $workspace_association_storage
->create([
'target_entity_type_id' => $entity
->getEntityTypeId(),
'target_entity_id' => $entity
->id(),
]);
}
// Add the revision ID and the workspace ID.
$workspace_association
->set('target_entity_revision_id', $entity
->getRevisionId());
$workspace_association
->set('workspace', $this->workspaceManager
->getActiveWorkspace()
->id());
// Save without updating the tracked content entity.
$workspace_association
->save();
}