SessionWorkspaceNegotiator.php

Same filename in other branches
  1. 9 core/modules/workspaces/src/Negotiator/SessionWorkspaceNegotiator.php
  2. 8.9.x core/modules/workspaces/src/Negotiator/SessionWorkspaceNegotiator.php
  3. 11.x core/modules/workspaces/src/Negotiator/SessionWorkspaceNegotiator.php

Namespace

Drupal\workspaces\Negotiator

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;

/**
 * Defines the session workspace negotiator.
 */
class SessionWorkspaceNegotiator implements WorkspaceNegotiatorInterface, WorkspaceIdNegotiatorInterface {
    public function __construct(AccountInterface $currentUser, Session $session, EntityTypeManagerInterface $entityTypeManager) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function applies(Request $request) {
        // This negotiator only applies if the current user is authenticated.
        return $this->currentUser
            ->isAuthenticated();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getActiveWorkspaceId(Request $request) : ?string {
        return $this->session
            ->get('active_workspace_id');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getActiveWorkspace(Request $request) {
        $workspace_id = $this->getActiveWorkspaceId($request);
        if ($workspace_id && ($workspace = $this->entityTypeManager
            ->getStorage('workspace')
            ->load($workspace_id))) {
            return $workspace;
        }
        return NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setActiveWorkspace(WorkspaceInterface $workspace) {
        $this->session
            ->set('active_workspace_id', $workspace->id());
    }
    
    /**
     * {@inheritdoc}
     */
    public function unsetActiveWorkspace() {
        $this->session
            ->remove('active_workspace_id');
    }

}

Classes

Title Deprecated Summary
SessionWorkspaceNegotiator Defines the session workspace negotiator.

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