3 namespace Drupal\workspaces\Negotiator;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\workspaces\WorkspaceInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\Session\Session;
12 * Defines the session workspace negotiator.
14 class SessionWorkspaceNegotiator implements WorkspaceNegotiatorInterface {
19 * @var \Drupal\Core\Session\AccountInterface
21 protected $currentUser;
26 * @var \Symfony\Component\HttpFoundation\Session\Session
31 * The workspace storage handler.
33 * @var \Drupal\Core\Entity\EntityStorageInterface
35 protected $workspaceStorage;
40 * @param \Drupal\Core\Session\AccountInterface $current_user
42 * @param \Symfony\Component\HttpFoundation\Session\Session $session
44 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
45 * The entity type manager.
47 public function __construct(AccountInterface $current_user, Session $session, EntityTypeManagerInterface $entity_type_manager) {
48 $this->currentUser = $current_user;
49 $this->session = $session;
50 $this->workspaceStorage = $entity_type_manager->getStorage('workspace');
56 public function applies(Request $request) {
57 // This negotiator only applies if the current user is authenticated.
58 return $this->currentUser->isAuthenticated();
64 public function getActiveWorkspace(Request $request) {
65 $workspace_id = $this->session->get('active_workspace_id');
67 if ($workspace_id && ($workspace = $this->workspaceStorage->load($workspace_id))) {
77 public function setActiveWorkspace(WorkspaceInterface $workspace) {
78 $this->session->set('active_workspace_id', $workspace->id());