diff -u b/src/EntityTypeInfo.php b/src/EntityTypeInfo.php --- b/src/EntityTypeInfo.php +++ b/src/EntityTypeInfo.php @@ -189,2 +189,3 @@ } + } diff -u b/src/EventSubscriber/WorkbenchModerationSubscriber.php b/src/EventSubscriber/WorkbenchModerationSubscriber.php --- b/src/EventSubscriber/WorkbenchModerationSubscriber.php +++ b/src/EventSubscriber/WorkbenchModerationSubscriber.php @@ -81,13 +81,7 @@ $source_pointer = $this->getPointerToWorkspace($workspace); // Derive a replication task from the source Workspace. - $task = new ReplicationTask(); - $replication_settings = $workspace->get('push_replication_settings')->referencedEntities(); - $replication_settings = count($replication_settings) > 0 ? reset($replication_settings) : NULL; - if ($replication_settings !== NULL) { - $task->setFilter($replication_settings->getFilterId()); - $task->setParametersByArray($replication_settings->getParameters()); - } + $task = $this->replicatorManager->getTask($workspace, 'push'); $this->replicatorManager->replicate($source_pointer, $parent_workspace_pointer, $task); } diff -u b/src/Form/UpdateForm.php b/src/Form/UpdateForm.php --- b/src/Form/UpdateForm.php +++ b/src/Form/UpdateForm.php @@ -9,7 +9,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\replication\Entity\ReplicationLogInterface; -use Drupal\replication\ReplicationTask\ReplicationTask; class UpdateForm extends ConfirmFormBase { @@ -91,22 +90,9 @@ $active = $this->getActive(); try { // Derive a replication task from the source Workspace. - $task = new ReplicationTask(); - $replication_settings = $active - ->getWorkspace() - ->get('push_replication_settings') - ->referencedEntities(); - $replication_settings = count($replication_settings) > 0 ? reset($replication_settings) : NULL; - if ($replication_settings !== NULL) { - $task->setFilter($replication_settings->getFilterId()); - $task->setParametersByArray($replication_settings->getParameters()); - } + $task = $this->replicatorManager->getTask($active->getWorkspace(), 'push'); - $response = \Drupal::service('workspace.replicator_manager')->update( - $upstream, - $active, - $task - ); + $response = $this->replicatorManager->update($upstream, $active, $task); if (($response instanceof ReplicationLogInterface) && $response->get('ok')) { drupal_set_message($this->t('%workspace has been updated with content from %upstream.', ['%upstream' => $upstream->label(), '%workspace' => $active->label()])); diff -u b/src/Plugin/RulesAction/ReplicateContent.php b/src/Plugin/RulesAction/ReplicateContent.php --- b/src/Plugin/RulesAction/ReplicateContent.php +++ b/src/Plugin/RulesAction/ReplicateContent.php @@ -96,13 +96,7 @@ $target = $workspace->get('upstream')->entity; // Derive a replication task from the source Workspace. - $task = new ReplicationTask(); - $replication_settings = $source->get('push_replication_settings')->referencedEntities(); - $replication_settings = count($replication_settings) > 0 ? reset($replication_settings) : NULL; - if ($replication_settings !== NULL) { - $task->setFilter($replication_settings->getFilterId()); - $task->setParametersByArray($replication_settings->getParameters()); - } + $task = $this->replicatorManager->getTask($workspace, 'push'); /** @var \Drupal\replication\Entity\ReplicationLogInterface $result */ $result = $this->replicatorManager->replicate($source, $target, $task); diff -u b/src/ReplicatorManager.php b/src/ReplicatorManager.php --- b/src/ReplicatorManager.php +++ b/src/ReplicatorManager.php @@ -2,6 +2,8 @@ namespace Drupal\workspace; +use Drupal\Core\Field\EntityReferenceFieldItemListInterface; +use Drupal\multiversion\Entity\WorkspaceInterface; use Drupal\multiversion\Workspace\ConflictTrackerInterface; use Drupal\replication\Entity\ReplicationLog; use Drupal\replication\ReplicationTask\ReplicationTask; @@ -57,19 +59,10 @@ $initial_conflicts = $this->conflictTracker->getAll(); // Derive a replication task from the target Workspace for pulling. - $pull_task = new ReplicationTask(); - $replication_settings = $source - ->getWorkspace() - ->get('pull_replication_settings') - ->referencedEntities(); - $replication_settings = count($replication_settings) > 0 ? reset($replication_settings) : NULL; - if ($replication_settings !== NULL) { - $pull_task->setFilter($replication_settings->getFilterId()); - $pull_task->setParametersByArray($replication_settings->getParameters()); - } + $pull_task = $this->getTask($source->getWorkspace(), 'pull'); // Pull in changes from $target to $source to ensure a merge will complete. - $pull_log = $this->update($target, $source, $pull_task); + $this->update($target, $source, $pull_task); // @todo use $post_conflicts in a conflict management workflow $post_conflicts = $this->conflictTracker->getAll(); @@ -83,0 +77,22 @@ + * Returns a replication task. + * + * @param \Drupal\multiversion\Entity\WorkspaceInterface $workspace + * @param string $type + * The replication type ('pull' or 'push'). + * + * @return \Drupal\replication\ReplicationTask\ReplicationTaskInterface + */ + public function getTask(WorkspaceInterface $workspace, $type) { + $task = new ReplicationTask(); + $items = $workspace->get($type . '_replication_settings'); + if ($items instanceof EntityReferenceFieldItemListInterface) { + $referenced_entities = $items->referencedEntities(); + if (count($referenced_entities) > 0) { + $task->setFilter($referenced_entities[0]->getFilterId()); + $task->setParametersByArray($referenced_entities[0]->getParameters()); + } + } + return $task; + } + + /** diff -u b/tests/src/Functional/ReplicationSettingsTest.php b/tests/src/Functional/ReplicationSettingsTest.php --- b/tests/src/Functional/ReplicationSettingsTest.php +++ b/tests/src/Functional/ReplicationSettingsTest.php @@ -2,9 +2,8 @@ namespace Drupal\Tests\workspace\Functional; -use Drupal\replication\ReplicationTask\ReplicationTask; use Drupal\simpletest\BlockCreationTrait; -use Drupal\simpletest\BrowserTestBase; +use Drupal\Tests\BrowserTestBase; /** * Test replication settings on replicate. @@ -19,6 +18,11 @@ placeBlock as drupalPlaceBlock; } + /** + * @var \Drupal\workspace\ReplicatorManager + */ + protected $replicatorManager; + public static $modules = [ 'system', 'node', @@ -31,6 +35,15 @@ ]; /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->replicatorManager = $this->container->get('workspace.replicator_manager'); + } + + /** * Verify pull replication settings using the published filter as an example. */ public function testPullReplicationSettings() { @@ -88,16 +101,10 @@ $target_pointer = $this->getPointerToWorkspace($target); // Derive a replication task from the target Workspace. - $task = new ReplicationTask(); - $replication_settings = $target->get('pull_replication_settings')->referencedEntities(); - $replication_settings = reset($replication_settings); - $task->setFilter($replication_settings->getFilterId()); - $task->setParametersByArray($replication_settings->getParameters()); + $task = $this->replicatorManager->getTask($target, 'pull'); // Replicate from Live to Target. - /** @var ReplicatorManager $replicator */ - $replicator = \Drupal::service('workspace.replicator_manager'); - $replicator->replicate($source_pointer, $target_pointer, $task); + $this->replicatorManager->replicate($source_pointer, $target_pointer, $task); // Verify the correct nodes were replicated. $this->switchToWorkspace($target); @@ -166,16 +173,10 @@ $this->assertTrue($this->isLabelInContentOverview('Unpublished node')); // Derive a replication task from the target Workspace. - $task = new ReplicationTask(); - $replication_settings = $target->get('push_replication_settings')->referencedEntities(); - $replication_settings = reset($replication_settings); - $task->setFilter($replication_settings->getFilterId()); - $task->setParametersByArray($replication_settings->getParameters()); + $task = $this->replicatorManager->getTask($target, 'push'); // Replicate from Target to Live. - /** @var ReplicatorManager $replicator */ - $replicator = \Drupal::service('workspace.replicator_manager'); - $replicator->replicate($target_pointer, $source_pointer, $task); + $this->replicatorManager->replicate($target_pointer, $source_pointer, $task); // Verify the correct nodes were replicated. $this->switchToWorkspace($live);