a5b697eedc2dae8406163df426dcbe62478feba5
[yaffs-website] / web / core / modules / workspaces / src / WorkspaceCacheContext.php
1 <?php
2
3 namespace Drupal\workspaces;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\Context\CacheContextInterface;
7
8 /**
9  * Defines the WorkspaceCacheContext service, for "per workspace" caching.
10  *
11  * Cache context ID: 'workspace'.
12  */
13 class WorkspaceCacheContext implements CacheContextInterface {
14
15   /**
16    * The workspace manager.
17    *
18    * @var \Drupal\workspaces\WorkspaceManagerInterface
19    */
20   protected $workspaceManager;
21
22   /**
23    * Constructs a new WorkspaceCacheContext service.
24    *
25    * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
26    *   The workspace manager.
27    */
28   public function __construct(WorkspaceManagerInterface $workspace_manager) {
29     $this->workspaceManager = $workspace_manager;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function getLabel() {
36     return t('Workspace');
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function getContext() {
43     return $this->workspaceManager->getActiveWorkspace()->id();
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function getCacheableMetadata($type = NULL) {
50     // The active workspace will always be stored in the user's session.
51     $cacheability = new CacheableMetadata();
52     $cacheability->addCacheContexts(['session']);
53
54     return $cacheability;
55   }
56
57 }