You are here

public function WorkspaceManager::purgeDeletedWorkspacesBatch in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::purgeDeletedWorkspacesBatch()
  2. 10 core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::purgeDeletedWorkspacesBatch()

Deletes the revisions associated with deleted workspaces.

Overrides WorkspaceManagerInterface::purgeDeletedWorkspacesBatch

File

core/modules/workspaces/src/WorkspaceManager.php, line 306

Class

WorkspaceManager
Provides the workspace manager.

Namespace

Drupal\workspaces

Code

public function purgeDeletedWorkspacesBatch() {
  $deleted_workspace_ids = $this->state
    ->get('workspace.deleted', []);

  // Bail out early if there are no workspaces to purge.
  if (empty($deleted_workspace_ids)) {
    return;
  }
  $batch_size = Settings::get('entity_update_batch_size', 50);

  // Get the first deleted workspace from the list and delete the revisions
  // associated with it, along with the workspace association records.
  $workspace_id = reset($deleted_workspace_ids);
  $tracked_entities = $this->workspaceAssociation
    ->getTrackedEntities($workspace_id);
  $count = 1;
  foreach ($tracked_entities as $entity_type_id => $entities) {
    $associated_entity_storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
    $associated_revisions = $this->workspaceAssociation
      ->getAssociatedRevisions($workspace_id, $entity_type_id);
    foreach (array_keys($associated_revisions) as $revision_id) {
      if ($count > $batch_size) {
        continue 2;
      }

      // Delete the associated entity revision.
      $associated_entity_storage
        ->deleteRevision($revision_id);
      $count++;
    }

    // Delete the workspace association entries.
    $this->workspaceAssociation
      ->deleteAssociations($workspace_id, $entity_type_id, $entities);
  }

  // The purging operation above might have taken a long time, so we need to
  // request a fresh list of tracked entities. If it is empty, we can go ahead
  // and remove the deleted workspace ID entry from state.
  if (!$this->workspaceAssociation
    ->getTrackedEntities($workspace_id)) {
    unset($deleted_workspace_ids[$workspace_id]);
    $this->state
      ->set('workspace.deleted', $deleted_workspace_ids);
  }
}