function WorkspaceManager::executeInWorkspace

Same name and namespace in other branches
  1. 11.x core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::executeInWorkspace()
  2. 10 core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::executeInWorkspace()
  3. 8.9.x core/modules/workspaces/src/WorkspaceManager.php \Drupal\workspaces\WorkspaceManager::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;
}

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