class WorkspacePublisher
Same name in other branches
- 9 core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher
- 8.9.x core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher
- 11.x core/modules/workspaces/src/WorkspacePublisher.php \Drupal\workspaces\WorkspacePublisher
Default implementation of the workspace publisher.
@internal
Hierarchy
- class \Drupal\workspaces\WorkspacePublisher implements \Drupal\workspaces\WorkspacePublisherInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of WorkspacePublisher
File
-
core/
modules/ workspaces/ src/ WorkspacePublisher.php, line 19
Namespace
Drupal\workspacesView source
class WorkspacePublisher implements WorkspacePublisherInterface {
use StringTranslationTrait;
/**
* The source workspace entity.
*
* @var \Drupal\workspaces\WorkspaceInterface
*/
protected $sourceWorkspace;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The workspace manager.
*
* @var \Drupal\workspaces\WorkspaceManagerInterface
*/
protected $workspaceManager;
/**
* The workspace association service.
*
* @var \Drupal\workspaces\WorkspaceAssociationInterface
*/
protected $workspaceAssociation;
/**
* The event dispatcher.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a new WorkspacePublisher.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Database\Connection $database
* Database connection.
* @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
* The workspace manager.
* @param \Drupal\workspaces\WorkspaceAssociationInterface $workspace_association
* The workspace association service.
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\workspaces\WorkspaceInterface $source
* The source workspace entity.
* @param \Psr\Log\LoggerInterface|null $logger
* The logger.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, WorkspaceManagerInterface $workspace_manager, WorkspaceAssociationInterface $workspace_association, $event_dispatcher, ?WorkspaceInterface $source = NULL, ?LoggerInterface $logger = NULL) {
$this->entityTypeManager = $entity_type_manager;
$this->database = $database;
$this->workspaceManager = $workspace_manager;
$this->workspaceAssociation = $workspace_association;
if ($event_dispatcher instanceof WorkspaceInterface) {
@trigger_error('Calling WorkspacePublisher::__construct() without the $event_dispatcher argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3242573', E_USER_DEPRECATED);
$source = $event_dispatcher;
$event_dispatcher = \Drupal::service('event_dispatcher');
}
$this->eventDispatcher = $event_dispatcher;
$this->sourceWorkspace = $source;
if ($this->logger === NULL) {
@trigger_error('Calling ' . __METHOD__ . '() without the $logger argument is deprecated in drupal:10.1.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/2932520', E_USER_DEPRECATED);
$this->logger = \Drupal::service('logger.channel.workspaces');
}
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function getSourceLabel() {
return $this->sourceWorkspace
->label();
}
/**
* {@inheritdoc}
*/
public function getTargetLabel() {
return $this->t('Live');
}
/**
* {@inheritdoc}
*/
public function checkConflictsOnTarget() {
// Nothing to do for now, we can not get to a conflicting state because an
// entity which is being edited in a workspace can not be edited in any
// other workspace.
}
/**
* {@inheritdoc}
*/
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);
// Get the latest revision IDs for all the entities that are tracked by
// the source workspace.
$query = $this->entityTypeManager
->getStorage($entity_type_id)
->getQuery()
->accessCheck(FALSE)
->condition($entity_type->getKey('id'), $tracked_revisions, 'IN')
->latestRevision();
$result = $query->execute();
// Now we compare the revision IDs which are tracked by the source
// workspace to the latest revision IDs of those entities and the
// difference between these two arrays gives us all the entities which
// have been modified on the target.
if ($revision_difference = array_diff_key($result, $tracked_revisions)) {
$target_revision_difference[$entity_type_id] = $revision_difference;
}
}
return $target_revision_difference;
}
/**
* {@inheritdoc}
*/
public function getDifferringRevisionIdsOnSource() {
// Get the Workspace association revisions which haven't been pushed yet.
return $this->workspaceAssociation
->getTrackedEntities($this->sourceWorkspace
->id());
}
/**
* {@inheritdoc}
*/
public function getNumberOfChangesOnTarget() {
$total_changes = $this->getDifferringRevisionIdsOnTarget();
return count($total_changes, COUNT_RECURSIVE) - count($total_changes);
}
/**
* {@inheritdoc}
*/
public function getNumberOfChangesOnSource() {
$total_changes = $this->getDifferringRevisionIdsOnSource();
return count($total_changes, COUNT_RECURSIVE) - count($total_changes);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. | ||
WorkspacePublisher::$database | protected | property | The database connection. | ||
WorkspacePublisher::$entityTypeManager | protected | property | The entity type manager. | ||
WorkspacePublisher::$eventDispatcher | protected | property | The event dispatcher. | ||
WorkspacePublisher::$sourceWorkspace | protected | property | The source workspace entity. | ||
WorkspacePublisher::$workspaceAssociation | protected | property | The workspace association service. | ||
WorkspacePublisher::$workspaceManager | protected | property | The workspace manager. | ||
WorkspacePublisher::checkConflictsOnTarget | public | function | Checks if there are any conflicts between the source and the target. | Overrides WorkspaceOperationInterface::checkConflictsOnTarget | |
WorkspacePublisher::getDifferringRevisionIdsOnSource | public | function | Gets the revision identifiers for items which have changed on the source. | Overrides WorkspaceOperationInterface::getDifferringRevisionIdsOnSource | |
WorkspacePublisher::getDifferringRevisionIdsOnTarget | public | function | Gets the revision identifiers for items which have changed on the target. | Overrides WorkspaceOperationInterface::getDifferringRevisionIdsOnTarget | |
WorkspacePublisher::getNumberOfChangesOnSource | public | function | Gets the total number of items which have changed on the source. | Overrides WorkspaceOperationInterface::getNumberOfChangesOnSource | |
WorkspacePublisher::getNumberOfChangesOnTarget | public | function | Gets the total number of items which have changed on the target. | Overrides WorkspaceOperationInterface::getNumberOfChangesOnTarget | |
WorkspacePublisher::getSourceLabel | public | function | Returns the human-readable label of the source. | Overrides WorkspaceOperationInterface::getSourceLabel | |
WorkspacePublisher::getTargetLabel | public | function | Returns the human-readable label of the target. | Overrides WorkspaceOperationInterface::getTargetLabel | |
WorkspacePublisher::publish | public | function | Publishes the contents of a workspace to the default (Live) workspace. | Overrides WorkspacePublisherInterface::publish | |
WorkspacePublisher::__construct | public | function | Constructs a new WorkspacePublisher. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.