WorkspaceController.php in Workspace 8
File
src/Controller/WorkspaceController.php
View source
<?php
namespace Drupal\workspace\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\multiversion\Entity\Workspace;
use Drupal\multiversion\Entity\WorkspaceType;
use Drupal\multiversion\Entity\WorkspaceTypeInterface;
use Drupal\workspace\Controller\Component\ConflictListBuilder;
use Drupal\workspace\Entity\WorkspacePointer;
class WorkspaceController extends ControllerBase {
protected $workspaceSettings;
public function __construct() {
$this->workspaceSettings = \Drupal::configFactory()
->getEditable('workspace.settings');
}
public function add() {
$types = WorkspaceType::loadMultiple();
if ($types && count($types) == 1) {
$type = reset($types);
return $this
->addForm($type);
}
if (count($types) === 0) {
return [
'#markup' => $this
->t('You have not created any Workspace types yet. Go to the <a href=":url">Workspace type creation page</a> to add a new Workspace type.', [
':url' => Url::fromRoute('entity.workspace_type.add')
->toString(),
]),
];
}
return [
'#theme' => 'workspace_add_list',
'#content' => $types,
];
}
public function addForm(WorkspaceTypeInterface $workspace_type) {
$upstream_id = $this->workspaceSettings
->get('upstream');
if (!$upstream_id) {
$upstream_id = $this
->getDefaultWorkspacePointer()
->id();
}
$workspace = Workspace::create([
'type' => $workspace_type
->id(),
'upstream' => $upstream_id,
'pull_replication_settings' => $this->workspaceSettings
->get('pull_replication_settings', ''),
'push_replication_settings' => $this->workspaceSettings
->get('push_replication_settings', ''),
]);
return $this
->entityFormBuilder()
->getForm($workspace);
}
public function getAddFormTitle(WorkspaceTypeInterface $workspace_type) {
return $this
->t('Add %type workspace', [
'%type' => $workspace_type
->label(),
]);
}
public function viewConflicts($workspace) {
$container = \Drupal::getContainer();
$builder = ConflictListBuilder::createInstance($container);
return $builder
->buildList($workspace);
}
public function getViewConflictsTitle() {
return 'Workspace Conflicts';
}
protected function getDefaultWorkspacePointer() {
$workspace_id = $this
->getDefaultWorkspaceId();
$workspace = Workspace::load($workspace_id);
return WorkspacePointer::loadFromWorkspace($workspace);
}
public static function getDefaultWorkspaceId() {
return \Drupal::getContainer()
->getParameter('workspace.default');
}
}