WorkspaceCacheContext.php

Same filename in other branches
  1. 9 core/modules/workspaces/src/WorkspaceCacheContext.php
  2. 10 core/modules/workspaces/src/WorkspaceCacheContext.php
  3. 11.x core/modules/workspaces/src/WorkspaceCacheContext.php

Namespace

Drupal\workspaces

File

core/modules/workspaces/src/WorkspaceCacheContext.php

View source
<?php

namespace Drupal\workspaces;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;

/**
 * Defines the WorkspaceCacheContext service, for "per workspace" caching.
 *
 * Cache context ID: 'workspace'.
 */
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;
    }

}

Classes

Title Deprecated Summary
WorkspaceCacheContext Defines the WorkspaceCacheContext service, for "per workspace" caching.

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