WorkspacePublisher.php in Drupal 10
File
core/modules/workspaces/src/WorkspacePublisher.php
View source
<?php
namespace Drupal\workspaces;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class WorkspacePublisher implements WorkspacePublisherInterface {
use StringTranslationTrait;
protected $sourceWorkspace;
protected $entityTypeManager;
protected $database;
protected $workspaceManager;
protected $workspaceAssociation;
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, WorkspaceManagerInterface $workspace_manager, WorkspaceAssociationInterface $workspace_association, WorkspaceInterface $source) {
$this->entityTypeManager = $entity_type_manager;
$this->database = $database;
$this->workspaceManager = $workspace_manager;
$this->workspaceAssociation = $workspace_association;
$this->sourceWorkspace = $source;
}
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();
}
$transaction = $this->database
->startTransaction();
try {
$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));
foreach ($entity_revisions as $entity) {
$entity
->setSyncing(TRUE);
$entity
->isDefaultRevision(TRUE);
$field_name = $entity
->getEntityType()
->getRevisionMetadataKey('workspace');
$entity->{$field_name}->target_id = NULL;
$entity->original = $default_revisions[$entity
->id()];
$entity
->save();
}
}
});
} catch (\Exception $e) {
$transaction
->rollBack();
watchdog_exception('workspaces', $e);
throw $e;
}
$this->workspaceAssociation
->postPublish($this->sourceWorkspace);
}
public function getSourceLabel() {
return $this->sourceWorkspace
->label();
}
public function getTargetLabel() {
return $this
->t('Live');
}
public function checkConflictsOnTarget() {
}
public function getDifferringRevisionIdsOnTarget() {
$target_revision_difference = [];
$tracked_entities = $this->workspaceAssociation
->getTrackedEntities($this->sourceWorkspace
->id());
foreach ($tracked_entities as $entity_type_id => $tracked_revisions) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$query = $this->entityTypeManager
->getStorage($entity_type_id)
->getQuery()
->accessCheck(FALSE)
->condition($entity_type
->getKey('id'), $tracked_revisions, 'IN')
->latestRevision();
$result = $query
->execute();
if ($revision_difference = array_diff_key($result, $tracked_revisions)) {
$target_revision_difference[$entity_type_id] = $revision_difference;
}
}
return $target_revision_difference;
}
public function getDifferringRevisionIdsOnSource() {
return $this->workspaceAssociation
->getTrackedEntities($this->sourceWorkspace
->id());
}
public function getNumberOfChangesOnTarget() {
$total_changes = $this
->getDifferringRevisionIdsOnTarget();
return count($total_changes, COUNT_RECURSIVE) - count($total_changes);
}
public function getNumberOfChangesOnSource() {
$total_changes = $this
->getDifferringRevisionIdsOnSource();
return count($total_changes, COUNT_RECURSIVE) - count($total_changes);
}
}