You are here

public function WorkspaceMerger::merge in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/WorkspaceMerger.php \Drupal\workspaces\WorkspaceMerger::merge()

Merges the contents of the source workspace into the target workspace.

Overrides WorkspaceMergerInterface::merge

File

core/modules/workspaces/src/WorkspaceMerger.php, line 86

Class

WorkspaceMerger
Default implementation of the workspace merger.

Namespace

Drupal\workspaces

Code

public function merge() {
  if (!$this->sourceWorkspace
    ->hasParent() || $this->sourceWorkspace->parent->target_id != $this->targetWorkspace
    ->id()) {
    throw new \InvalidArgumentException('The contents of a workspace can only be merged into its parent workspace.');
  }
  if ($this
    ->checkConflictsOnTarget()) {
    throw new WorkspaceConflictException();
  }
  $transaction = $this->database
    ->startTransaction();
  try {
    foreach ($this
      ->getDifferringRevisionIdsOnSource() as $entity_type_id => $revision_difference) {
      $revisions_on_source = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->loadMultipleRevisions(array_keys($revision_difference));

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      foreach ($revisions_on_source as $revision) {

        // Track all the differing revisions from the source workspace in
        // the context of the target workspace. This will automatically
        // update all the descendants of the target workspace as well.
        $this->workspaceAssociation
          ->trackEntity($revision, $this->targetWorkspace);
      }

      // Since we're not saving entity objects, we need to invalidate the list
      // cache tags manually.
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      $this->cacheTagsInvalidator
        ->invalidateTags($entity_type
        ->getListCacheTags());
    }
  } catch (\Exception $e) {
    $transaction
      ->rollBack();
    watchdog_exception('workspaces', $e);
    throw $e;
  }
}