function WorkspaceManager::executeInWorkspace

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::executeInWorkspace()
  2. 8.9.x core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::executeInWorkspace()
  3. 11.x core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::executeInWorkspace()

Executes the given callback function in the context of a workspace.

Parameters

string $workspace_id: The ID of a workspace.

callable $function: The callback to be executed.

Return value

mixed The callable's return value.

Overrides WorkspaceManagerInterface::executeInWorkspace

File

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

Class

WorkspaceManager
Provides the workspace manager.

Namespace

Drupal\workspaces

Code

public function executeInWorkspace($workspace_id, callable $function) {
  /** @var \Drupal\workspaces\WorkspaceInterface $workspace */
  $workspace = $this->entityTypeManager
    ->getStorage('workspace')
    ->load($workspace_id);
  if (!$workspace) {
    throw new \InvalidArgumentException('The ' . $workspace_id . ' workspace does not exist.');
  }
  $previous_active_workspace = $this->getActiveWorkspace();
  $this->doSwitchWorkspace($workspace);
  $result = $function();
  $this->doSwitchWorkspace($previous_active_workspace);
  return $result;
}

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