class WorkspaceCacheContext

Same name in other branches
  1. 9 core/modules/workspaces/src/WorkspaceCacheContext.php \Drupal\workspaces\WorkspaceCacheContext
  2. 8.9.x core/modules/workspaces/src/WorkspaceCacheContext.php \Drupal\workspaces\WorkspaceCacheContext
  3. 10 core/modules/workspaces/src/WorkspaceCacheContext.php \Drupal\workspaces\WorkspaceCacheContext

Defines the WorkspaceCacheContext service, for "per workspace" caching.

Cache context ID: 'workspace'.

Hierarchy

Expanded class hierarchy of WorkspaceCacheContext

1 file declares its use of WorkspaceCacheContext
WorkspaceCacheContextTest.php in core/modules/workspaces/tests/src/Functional/WorkspaceCacheContextTest.php
1 string reference to 'WorkspaceCacheContext'
workspaces.services.yml in core/modules/workspaces/workspaces.services.yml
core/modules/workspaces/workspaces.services.yml
1 service uses WorkspaceCacheContext
cache_context.workspace in core/modules/workspaces/workspaces.services.yml
Drupal\workspaces\WorkspaceCacheContext

File

core/modules/workspaces/src/WorkspaceCacheContext.php, line 13

Namespace

Drupal\workspaces
View source
class WorkspaceCacheContext implements CacheContextInterface {
    
    /**
     * The workspace manager.
     *
     * @var \Drupal\workspaces\WorkspaceManagerInterface
     */
    protected $workspaceManager;
    
    /**
     * Constructs a new WorkspaceCacheContext service.
     *
     * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
     *   The workspace manager.
     */
    public function __construct(WorkspaceManagerInterface $workspace_manager) {
        $this->workspaceManager = $workspace_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getLabel() {
        return t('Workspace');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext() {
        return $this->workspaceManager
            ->hasActiveWorkspace() ? $this->workspaceManager
            ->getActiveWorkspace()
            ->id() : 'live';
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata($type = NULL) {
        // The active workspace will always be stored in the user's session.
        $cacheability = new CacheableMetadata();
        $cacheability->addCacheContexts([
            'session',
        ]);
        return $cacheability;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
WorkspaceCacheContext::$workspaceManager protected property The workspace manager.
WorkspaceCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
WorkspaceCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
WorkspaceCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
WorkspaceCacheContext::__construct public function Constructs a new WorkspaceCacheContext service.

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