You are here

public function WorkspaceManager::setActiveWorkspace in Workspace 8.2

Sets the active workspace via the workspace negotiators.

Parameters

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

Return value

$this

Throws

\Drupal\workspace\WorkspaceAccessException Thrown when the current user doesn't have access to view the workspace.

Overrides WorkspaceManagerInterface::setActiveWorkspace

File

src/WorkspaceManager.php, line 169

Class

WorkspaceManager
Provides the workspace manager.

Namespace

Drupal\workspace

Code

public function setActiveWorkspace(WorkspaceInterface $workspace) {

  // If the current user doesn't have access to view the workspace, they
  // shouldn't be allowed to switch to it.
  if (!$workspace
    ->access('view') && !$workspace
    ->isDefaultWorkspace()) {
    $this->logger
      ->error('Denied access to view workspace %workspace_label for user %uid', [
      '%workspace_label' => $workspace
        ->label(),
      '%uid' => $this->currentUser
        ->id(),
    ]);
    throw new WorkspaceAccessException('The user does not have permission to view that workspace.');
  }
  $this->activeWorkspace = $workspace;

  // Set the workspace on the proper negotiator.
  $request = $this->requestStack
    ->getCurrentRequest();
  foreach ($this->negotiatorIds as $negotiator_id) {
    $negotiator = $this->classResolver
      ->getInstanceFromDefinition($negotiator_id);
    if ($negotiator
      ->applies($request)) {
      $negotiator
        ->setActiveWorkspace($workspace);
      break;
    }
  }
  $supported_entity_types = $this
    ->getSupportedEntityTypes();
  foreach ($supported_entity_types as $supported_entity_type) {
    $this->entityTypeManager
      ->getStorage($supported_entity_type
      ->id())
      ->resetCache();
  }
  return $this;
}