ReplicateContent.php in Workspace 8
File
src/Plugin/RulesAction/ReplicateContent.php
View source
<?php
namespace Drupal\workspace\Plugin\RulesAction;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\multiversion\MultiversionManagerInterface;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Drupal\rules\Core\RulesActionBase;
use Drupal\workspace\ReplicatorManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReplicateContent extends RulesActionBase implements ContainerFactoryPluginInterface {
protected $workspaceManager;
protected $entityTypeManager;
protected $replicatorManager;
protected $multiversionManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager, ReplicatorManager $replicator_manager, MultiversionManagerInterface $multiversion_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->workspaceManager = $workspace_manager;
$this->entityTypeManager = $entity_type_manager;
$this->replicatorManager = $replicator_manager;
$this->multiversionManager = $multiversion_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('workspace.manager'), $container
->get('entity_type.manager'), $container
->get('workspace.replicator_manager'), $container
->get('multiversion.manager'));
}
protected function doExecute(EntityInterface $entity) {
$workspace = $this->multiversionManager
->isSupportedEntityType($entity
->getEntityType()) ? $entity
->get('workspace')->entity : $this->workspaceManager
->getActiveWorkspace();
$source = $this
->getPointerToWorkspace($workspace);
$upstream = $workspace
->get('upstream')->entity;
$task = $this->replicatorManager
->getTask($workspace, 'push_replication_settings');
$result = $this->replicatorManager
->replicate($source, $upstream, $task);
if ($result
->get('ok')->value == TRUE) {
drupal_set_message($this
->t('Content replicated from workspace @source to workspace @upstream.', [
'@source' => $workspace
->label(),
'@upstream' => $upstream
->label(),
]));
}
else {
drupal_set_message($this
->t('Error replicating content.'));
}
}
protected function getPointerToWorkspace(WorkspaceInterface $workspace) {
$pointers = $this->entityTypeManager
->getStorage('workspace_pointer')
->loadByProperties([
'workspace_pointer' => $workspace
->id(),
]);
$pointer = reset($pointers);
return $pointer;
}
}