function WorkspacePublisher::publish

Same name in other branches
  1. 9 core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher::publish()
  2. 8.9.x core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher::publish()
  3. 11.x core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher::publish()

Overrides WorkspacePublisherInterface::publish

File

core/modules/workspaces/src/WorkspacePublisher.php, line 104

Class

WorkspacePublisher
Default implementation of the workspace publisher.

Namespace

Drupal\workspaces

Code

public function publish() {
    if ($this->sourceWorkspace
        ->hasParent()) {
        throw new WorkspacePublishException('Only top-level workspaces can be published.');
    }
    if ($this->checkConflictsOnTarget()) {
        throw new WorkspaceConflictException();
    }
    $tracked_entities = $this->workspaceAssociation
        ->getTrackedEntities($this->sourceWorkspace
        ->id());
    $event = new WorkspacePrePublishEvent($this->sourceWorkspace, $tracked_entities);
    $this->eventDispatcher
        ->dispatch($event);
    if ($event->isPublishingStopped()) {
        throw new WorkspacePublishException((string) $event->getPublishingStoppedReason());
    }
    try {
        $transaction = $this->database
            ->startTransaction();
        $this->workspaceManager
            ->executeOutsideWorkspace(function () use ($tracked_entities) {
            $max_execution_time = ini_get('max_execution_time');
            $step_size = Settings::get('entity_update_batch_size', 50);
            $counter = 0;
            foreach ($tracked_entities as $entity_type_id => $revision_difference) {
                $entity_revisions = $this->entityTypeManager
                    ->getStorage($entity_type_id)
                    ->loadMultipleRevisions(array_keys($revision_difference));
                $default_revisions = $this->entityTypeManager
                    ->getStorage($entity_type_id)
                    ->loadMultiple(array_values($revision_difference));
                
                /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
                foreach ($entity_revisions as $entity) {
                    // When pushing workspace-specific revisions to the default
                    // workspace (Live), we simply need to mark them as default
                    // revisions.
                    $entity->setSyncing(TRUE);
                    $entity->isDefaultRevision(TRUE);
                    // The default revision is not workspace-specific anymore.
                    $field_name = $entity->getEntityType()
                        ->getRevisionMetadataKey('workspace');
                    $entity->{$field_name}->target_id = NULL;
                    $entity->original = $default_revisions[$entity->id()];
                    $entity->save();
                    $counter++;
                    // Extend the execution time in order to allow processing workspaces
                    // that contain a large number of items.
                    if ((int) ($counter / $step_size) >= 1) {
                        set_time_limit($max_execution_time);
                        $counter = 0;
                    }
                }
            }
        });
    } catch (\Exception $e) {
        if (isset($transaction)) {
            $transaction->rollBack();
        }
        Error::logException($this->logger, $e);
        throw $e;
    }
    $event = new WorkspacePostPublishEvent($this->sourceWorkspace, $tracked_entities);
    $this->eventDispatcher
        ->dispatch($event);
}

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