function WorkspacePublisher::publish

Same name in other branches
  1. 8.9.x core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher::publish()
  2. 10 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 79

Class

WorkspacePublisher
Default implementation of the workspace publisher.

Namespace

Drupal\workspaces

Code

public function publish() {
    $publish_access = $this->sourceWorkspace
        ->access('publish', NULL, TRUE);
    if (!$publish_access->isAllowed()) {
        $message = $publish_access instanceof AccessResultReasonInterface ? $publish_access->getReason() : '';
        throw new WorkspaceAccessException($message);
    }
    if ($this->checkConflictsOnTarget()) {
        throw new WorkspaceConflictException();
    }
    try {
        $transaction = $this->database
            ->startTransaction();
        // @todo Handle the publishing of a workspace with a batch operation in
        //   https://www.drupal.org/node/2958752.
        $this->workspaceManager
            ->executeOutsideWorkspace(function () {
            foreach ($this->getDifferringRevisionIdsOnSource() 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();
                }
            }
        });
    } catch (\Exception $e) {
        if (isset($transaction)) {
            $transaction->rollBack();
        }
        watchdog_exception('workspaces', $e);
        throw $e;
    }
    // Notify the workspace association that a workspace has been published.
    $this->workspaceAssociation
        ->postPublish($this->sourceWorkspace);
}

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