You are here

class WorkspaceCacheContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/WorkspaceCacheContext.php \Drupal\workspaces\WorkspaceCacheContext
  2. 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

Namesort descending Modifiers Type Description Overrides
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.