comparison core/modules/workspaces/src/WorkspacePublisher.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
46 * @var \Drupal\workspaces\WorkspaceAssociationStorageInterface 46 * @var \Drupal\workspaces\WorkspaceAssociationStorageInterface
47 */ 47 */
48 protected $workspaceAssociationStorage; 48 protected $workspaceAssociationStorage;
49 49
50 /** 50 /**
51 * The workspace manager.
52 *
53 * @var \Drupal\workspaces\WorkspaceManagerInterface
54 */
55 protected $workspaceManager;
56
57 /**
51 * Constructs a new WorkspacePublisher. 58 * Constructs a new WorkspacePublisher.
52 * 59 *
53 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager 60 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
54 * The entity type manager. 61 * The entity type manager.
55 * @param \Drupal\Core\Database\Connection $database 62 * @param \Drupal\Core\Database\Connection $database
56 * Database connection. 63 * Database connection.
64 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
65 * The workspace manager.
57 */ 66 */
58 public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, WorkspaceInterface $source) { 67 public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, WorkspaceManagerInterface $workspace_manager, WorkspaceInterface $source) {
59 $this->entityTypeManager = $entity_type_manager; 68 $this->entityTypeManager = $entity_type_manager;
60 $this->database = $database; 69 $this->database = $database;
61 $this->workspaceAssociationStorage = $entity_type_manager->getStorage('workspace_association'); 70 $this->workspaceAssociationStorage = $entity_type_manager->getStorage('workspace_association');
71 $this->workspaceManager = $workspace_manager;
62 $this->sourceWorkspace = $source; 72 $this->sourceWorkspace = $source;
63 $this->targetWorkspace = $this->entityTypeManager->getStorage('workspace')->load(WorkspaceInterface::DEFAULT_WORKSPACE); 73 $this->targetWorkspace = $this->entityTypeManager->getStorage('workspace')->load(WorkspaceInterface::DEFAULT_WORKSPACE);
64 } 74 }
65 75
66 /** 76 /**
73 83
74 $transaction = $this->database->startTransaction(); 84 $transaction = $this->database->startTransaction();
75 try { 85 try {
76 // @todo Handle the publishing of a workspace with a batch operation in 86 // @todo Handle the publishing of a workspace with a batch operation in
77 // https://www.drupal.org/node/2958752. 87 // https://www.drupal.org/node/2958752.
78 foreach ($this->getDifferringRevisionIdsOnSource() as $entity_type_id => $revision_difference) { 88 $this->workspaceManager->executeInWorkspace($this->targetWorkspace->id(), function () {
79 $entity_revisions = $this->entityTypeManager->getStorage($entity_type_id) 89 foreach ($this->getDifferringRevisionIdsOnSource() as $entity_type_id => $revision_difference) {
80 ->loadMultipleRevisions(array_keys($revision_difference)); 90
81 /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $entity */ 91 $entity_revisions = $this->entityTypeManager->getStorage($entity_type_id)
82 foreach ($entity_revisions as $entity) { 92 ->loadMultipleRevisions(array_keys($revision_difference));
83 // When pushing workspace-specific revisions to the default workspace 93 $default_revisions = $this->entityTypeManager->getStorage($entity_type_id)
84 // (Live), we simply need to mark them as default revisions. 94 ->loadMultiple(array_values($revision_difference));
85 // @todo Remove this dynamic property once we have an API for 95
86 // associating temporary data with an entity: 96 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
87 // https://www.drupal.org/node/2896474. 97 foreach ($entity_revisions as $entity) {
88 $entity->_isReplicating = TRUE; 98 // When pushing workspace-specific revisions to the default
89 $entity->isDefaultRevision(TRUE); 99 // workspace (Live), we simply need to mark them as default
90 $entity->save(); 100 // revisions.
101 $entity->setSyncing(TRUE);
102 $entity->isDefaultRevision(TRUE);
103 $entity->original = $default_revisions[$entity->id()];
104 $entity->save();
105 }
91 } 106 }
92 } 107 });
93 } 108 }
94 catch (\Exception $e) { 109 catch (\Exception $e) {
95 $transaction->rollBack(); 110 $transaction->rollBack();
96 watchdog_exception('workspaces', $e); 111 watchdog_exception('workspaces', $e);
97 throw $e; 112 throw $e;