Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / workspaces / src / WorkspaceCacheContext.php
diff --git a/web/core/modules/workspaces/src/WorkspaceCacheContext.php b/web/core/modules/workspaces/src/WorkspaceCacheContext.php
new file mode 100644 (file)
index 0000000..a5b697e
--- /dev/null
@@ -0,0 +1,57 @@
+<?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->getActiveWorkspace()->id();
+  }
+
+  /**
+   * {@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;
+  }
+
+}