You are here

public function WorkspaceManager::setActiveWorkspace in Multiversion 8

Sets the active workspace for the site/session.

Parameters

\Drupal\multiversion\Entity\WorkspaceInterface $workspace: The workspace to set as active.

Return value

\Drupal\multiversion\Workspace\WorkspaceManagerInterface

Throws

WorkspaceAccessException

Overrides WorkspaceManagerInterface::setActiveWorkspace

File

src/Workspace/WorkspaceManager.php, line 127

Class

WorkspaceManager

Namespace

Drupal\multiversion\Workspace

Code

public function setActiveWorkspace(WorkspaceInterface $workspace) {

  // Unpublished workspaces should not be allowed to be active.
  if (!$workspace
    ->isPublished()) {
    $this->logger
      ->error('The workspace {workspace} has been archived.', [
      'workspace' => $workspace
        ->label(),
    ]);
    throw new InvalidParameterException('Archived workspaces cannot be set as the active workspace.');
  }

  // If the current user doesn't have access to view the workspace, they
  // shouldn't be allowed to switch to it.
  // @todo Could this be handled better?
  if (!$workspace
    ->access('view') && !$workspace
    ->isDefaultWorkspace()) {
    $this->logger
      ->error('Denied access to view workspace {workspace}', [
      'workspace' => $workspace
        ->label(),
    ]);
    throw new WorkspaceAccessException('The user does not have permission to view that workspace.');
  }

  // Set the workspace on the proper negotiator.
  $request = $this->requestStack
    ->getCurrentRequest();
  foreach ($this
    ->getSortedNegotiators() as $negotiator) {
    if ($negotiator
      ->applies($request)) {
      $negotiator
        ->persist($workspace);
      break;
    }
  }

  // Clear cached entity storage handlers
  $this->entityTypeManager
    ->clearCachedDefinitions();
  return $this;
}