You are here

public function WorkspaceManager::executeInWorkspace in Drupal 9

Same name and namespace in other branches
  1. 8 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 268

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;
}