SessionWorkspaceNegotiator.php in Drupal 9
File
core/modules/workspaces/src/Negotiator/SessionWorkspaceNegotiator.php
View source
<?php
namespace Drupal\workspaces\Negotiator;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workspaces\WorkspaceInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
class SessionWorkspaceNegotiator implements WorkspaceNegotiatorInterface {
protected $currentUser;
protected $session;
protected $workspaceStorage;
public function __construct(AccountInterface $current_user, Session $session, EntityTypeManagerInterface $entity_type_manager) {
$this->currentUser = $current_user;
$this->session = $session;
$this->workspaceStorage = $entity_type_manager
->getStorage('workspace');
}
public function applies(Request $request) {
return $this->currentUser
->isAuthenticated();
}
public function getActiveWorkspace(Request $request) {
$workspace_id = $this->session
->get('active_workspace_id');
if ($workspace_id && ($workspace = $this->workspaceStorage
->load($workspace_id))) {
return $workspace;
}
return NULL;
}
public function setActiveWorkspace(WorkspaceInterface $workspace) {
$this->session
->set('active_workspace_id', $workspace
->id());
}
public function unsetActiveWorkspace() {
$this->session
->remove('active_workspace_id');
}
}