function WorkspaceManager::getActiveWorkspace

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::getActiveWorkspace()
  2. 8.9.x core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::getActiveWorkspace()
  3. 11.x 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) {
      /** @var \Drupal\workspaces\Negotiator\WorkspaceIdNegotiatorInterface $negotiator */
      $negotiator = $this->classResolver
        ->getInstanceFromDefinition($negotiator_id);
      if ($negotiator->applies($request)) {
        if ($workspace_id = $negotiator->getActiveWorkspaceId($request)) {
          /** @var \Drupal\workspaces\WorkspaceInterface $negotiated_workspace */
          $negotiated_workspace = $this->entityTypeManager
            ->getStorage('workspace')
            ->load($workspace_id);
        }
        // By default, 'view' access is checked when a workspace is activated,
        // but it should also be checked when retrieving the currently active
        // workspace.
        if (isset($negotiated_workspace) && $negotiated_workspace->access('view')) {
          // Notify the negotiator that its workspace has been selected.
          $negotiator->setActiveWorkspace($negotiated_workspace);
          $active_workspace = $negotiated_workspace;
          break;

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.