function WorkspaceAssociation::trackEntity

Same name in other branches
  1. 8.9.x core/modules/workspaces/src/WorkspaceAssociation.php \Drupal\workspaces\WorkspaceAssociation::trackEntity()
  2. 10 core/modules/workspaces/src/WorkspaceAssociation.php \Drupal\workspaces\WorkspaceAssociation::trackEntity()
  3. 11.x core/modules/workspaces/src/WorkspaceAssociation.php \Drupal\workspaces\WorkspaceAssociation::trackEntity()

Overrides WorkspaceAssociationInterface::trackEntity

File

core/modules/workspaces/src/WorkspaceAssociation.php, line 60

Class

WorkspaceAssociation
Provides a class for CRUD operations on workspace associations.

Namespace

Drupal\workspaces

Code

public function trackEntity(RevisionableInterface $entity, WorkspaceInterface $workspace) {
    // Determine all workspaces that might be affected by this change.
    $affected_workspaces = $this->workspaceRepository
        ->getDescendantsAndSelf($workspace->id());
    // Get the currently tracked revision for this workspace.
    $tracked = $this->getTrackedEntities($workspace->id(), $entity->getEntityTypeId(), [
        $entity->id(),
    ]);
    $tracked_revision_id = NULL;
    if (isset($tracked[$entity->getEntityTypeId()])) {
        $tracked_revision_id = key($tracked[$entity->getEntityTypeId()]);
    }
    try {
        $transaction = $this->database
            ->startTransaction();
        // Update all affected workspaces that were tracking the current revision.
        // This means they are inheriting content and should be updated.
        if ($tracked_revision_id) {
            $this->database
                ->update(static::TABLE)
                ->fields([
                'target_entity_revision_id' => $entity->getRevisionId(),
            ])
                ->condition('workspace', $affected_workspaces, 'IN')
                ->condition('target_entity_type_id', $entity->getEntityTypeId())
                ->condition('target_entity_id', $entity->id())
                ->condition('target_entity_revision_id', $tracked_revision_id)
                ->execute();
        }
        // Insert a new index entry for each workspace that is not tracking this
        // entity yet.
        $missing_workspaces = array_diff($affected_workspaces, $this->getEntityTrackingWorkspaceIds($entity));
        if ($missing_workspaces) {
            $insert_query = $this->database
                ->insert(static::TABLE)
                ->fields([
                'workspace',
                'target_entity_revision_id',
                'target_entity_type_id',
                'target_entity_id',
            ]);
            foreach ($missing_workspaces as $workspace_id) {
                $insert_query->values([
                    'workspace' => $workspace_id,
                    'target_entity_type_id' => $entity->getEntityTypeId(),
                    'target_entity_id' => $entity->id(),
                    'target_entity_revision_id' => $entity->getRevisionId(),
                ]);
            }
            $insert_query->execute();
        }
    } catch (\Exception $e) {
        if (isset($transaction)) {
            $transaction->rollBack();
        }
        watchdog_exception('workspaces', $e);
        throw $e;
    }
}

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