View source
<?php
namespace Drupal\workspace\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\PrependCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\multiversion\Workspace\ConflictTrackerInterface;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Drupal\replication\Entity\ReplicationLogInterface;
use Drupal\workspace\ReplicatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UpdateForm extends ConfirmFormBase {
protected $workspaceManager;
protected $entityTypeManager;
protected $replicatorManager;
protected $renderer;
protected $conflictTracker;
public function __construct(WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager, ReplicatorInterface $replicator_manager, RendererInterface $renderer, ConflictTrackerInterface $conflict_tracker) {
$this->workspaceManager = $workspace_manager;
$this->entityTypeManager = $entity_type_manager;
$this->replicatorManager = $replicator_manager;
$this->renderer = $renderer;
$this->conflictTracker = $conflict_tracker;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('workspace.manager'), $container
->get('entity_type.manager'), $container
->get('workspace.replicator_manager'), $container
->get('renderer'), $container
->get('workspace.conflict_tracker'));
}
protected function getActive() {
$workspace = $this->workspaceManager
->getActiveWorkspace();
$pointers = $this->entityTypeManager
->getStorage('workspace_pointer')
->loadByProperties([
'workspace_pointer' => $workspace
->id(),
]);
return reset($pointers);
}
protected function getUpstream() {
$workspace = $this->workspaceManager
->getActiveWorkspace();
if (isset($workspace->upstream)) {
return $workspace->upstream->entity;
}
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [
'callback' => [
$this,
'update',
],
'event' => 'mousedown',
'prevent' => 'click',
'progress' => [
'type' => 'throbber',
'message' => 'Updating',
],
];
if (!$this
->getUpstream()) {
unset($form['actions']['submit']);
}
unset($form['actions']['cancel']);
return $form;
}
public function getQuestion() {
return $this
->t('Queue an update of @workspace', [
'@workspace' => $this
->getActive()
->label(),
]);
}
public function getCancelUrl() {
return new Url('system.admin');
}
public function getFormId() {
return 'workspace_update_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$upstream = $this
->getUpstream();
$active = $this
->getActive();
try {
$task = $this->replicatorManager
->getTask($active
->getWorkspace(), 'pull_replication_settings');
$response = $this->replicatorManager
->update($upstream, $active, $task);
if ($response instanceof ReplicationLogInterface && $response
->get('ok')->value == TRUE) {
$conflicts = $this->conflictTracker
->useWorkspace($active
->getWorkspace())
->getAll();
if ($conflicts) {
drupal_set_message($this
->t('%workspace has been updated with content from %upstream, but there are <a href=":link">@count conflict(s) with the %target workspace</a>.', [
'%upstream' => $upstream
->label(),
'%workspace' => $active
->label(),
':link' => Url::fromRoute('entity.workspace.conflicts', [
'workspace' => $active
->getWorkspace()
->id(),
])
->toString(),
'@count' => count($conflicts),
'%target' => $upstream
->label(),
]), 'error');
}
else {
drupal_set_message($this
->t('An update of %workspace has been queued with content from %upstream.', [
'%upstream' => $upstream
->label(),
'%workspace' => $active
->label(),
]));
if (\Drupal::moduleHandler()
->moduleExists('deploy')) {
$input = $form_state
->getUserInput();
if (!isset($input['_drupal_ajax'])) {
$form_state
->setRedirect('entity.replication.collection');
}
}
}
}
else {
drupal_set_message($this
->t('Error updating %workspace from %upstream.', [
'%upstream' => $upstream
->label(),
'%workspace' => $active
->label(),
]), 'error');
}
} catch (\Exception $e) {
watchdog_exception('Workspace', $e);
drupal_set_message($e
->getMessage(), 'error');
}
}
public function update(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
if (\Drupal::moduleHandler()
->moduleExists('deploy')) {
$response
->addCommand(new RedirectCommand(Url::fromRoute('entity.replication.collection')
->setAbsolute()
->toString()));
}
else {
$status_messages = [
'#type' => 'status_messages',
];
$response
->addCommand(new PrependCommand('.region-highlighted', $this->renderer
->renderRoot($status_messages)));
}
return $response;
}
public function getDescription() {
if (!$this
->getUpstream()) {
return $this
->t('%workspace has no upstream set.', [
'%workspace' => $this
->getActive()
->label(),
]);
}
return $this
->t('Do you want to queue %workspace to be updated with changes from %upstream?', [
'%upstream' => $this
->getUpstream()
->label(),
'%workspace' => $this
->getActive()
->label(),
]);
}
}