You are here

public function WorkspaceManager::getActiveWorkspace in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::getActiveWorkspace()

Gets the active workspace.

Return value

\Drupal\workspaces\WorkspaceInterface The active workspace entity object.

Overrides WorkspaceManagerInterface::getActiveWorkspace

3 calls to WorkspaceManager::getActiveWorkspace()
WorkspaceManager::executeInWorkspace in core/modules/workspaces/src/WorkspaceManager.php
Executes the given callback function in the context of a workspace.
WorkspaceManager::executeOutsideWorkspace in core/modules/workspaces/src/WorkspaceManager.php
Executes the given callback function without any workspace context.
WorkspaceManager::hasActiveWorkspace in core/modules/workspaces/src/WorkspaceManager.php
Determines whether a workspace is active in the current request.

File

core/modules/workspaces/src/WorkspaceManager.php, line 173

Class

WorkspaceManager
Provides the workspace manager.

Namespace

Drupal\workspaces

Code

public function getActiveWorkspace() {
  if (!isset($this->activeWorkspace)) {
    $request = $this->requestStack
      ->getCurrentRequest();
    foreach ($this->negotiatorIds as $negotiator_id) {
      $negotiator = $this->classResolver
        ->getInstanceFromDefinition($negotiator_id);
      if ($negotiator
        ->applies($request)) {

        // By default, 'view' access is checked when a workspace is activated,
        // but it should also be checked when retrieving the currently active
        // workspace.
        if (($negotiated_workspace = $negotiator
          ->getActiveWorkspace($request)) && $negotiated_workspace
          ->access('view')) {
          $active_workspace = $negotiated_workspace;
          break;
        }
      }
    }

    // If no negotiator was able to determine the active workspace, default to
    // the live version of the site.
    $this->activeWorkspace = $active_workspace ?? FALSE;
  }
  return $this->activeWorkspace;
}