You are here

function workspace_multiversion_workspace_predelete in Workspace 8

Implements hook_multiversion_workspace_predelete().

Parameters

\Drupal\multiversion\Entity\WorkspaceInterface $workspace:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

./workspace.module, line 232

Code

function workspace_multiversion_workspace_predelete(WorkspaceInterface $workspace) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $workspace_pointer = WorkspacePointer::loadFromWorkspace($workspace);
  if (!empty($workspace_pointer)) {

    // Also mark as failed all deployments that have as source or target
    // the deleted workspace.
    $deployments = $entity_type_manager
      ->getStorage('replication')
      ->loadByProperties([
      'source' => $workspace_pointer
        ->id(),
    ]);
    $deployments += $entity_type_manager
      ->getStorage('replication')
      ->loadByProperties([
      'target' => $workspace_pointer
        ->id(),
    ]);

    /** @var Replication $deployment */
    foreach ($deployments as $deployment) {
      $replication_status = $deployment
        ->getReplicationStatus();
      if (!in_array($replication_status, [
        Replication::QUEUED,
        Replication::REPLICATING,
      ])) {
        continue;
      }
      $deployment
        ->setReplicationStatusFailed()
        ->setReplicationFailInfo(t('This deployment has been automatically marked as failed because source or target workspace has been deleted.'))
        ->save();
    }
    $workspace_pointer
      ->delete();
  }
}