class OgGroupContextCacheContext in Organic groups 8
Defines a cache context service for the currently active group.
This uses OgContext to determine the active group. Potential use cases for this cache context are elements on the page that vary by the active group, for example a group header, or a block showing recent group content.
Cache context ID: 'og_group_context'
Hierarchy
- class \Drupal\og\Cache\Context\OgGroupContextCacheContext implements CacheContextInterface
Expanded class hierarchy of OgGroupContextCacheContext
1 file declares its use of OgGroupContextCacheContext
- OgGroupContextCacheContextTest.php in tests/
src/ Unit/ Cache/ Context/ OgGroupContextCacheContextTest.php
1 string reference to 'OgGroupContextCacheContext'
1 service uses OgGroupContextCacheContext
File
- src/
Cache/ Context/ OgGroupContextCacheContext.php, line 21
Namespace
Drupal\og\Cache\ContextView source
class OgGroupContextCacheContext implements CacheContextInterface {
/**
* The string to return when no context is found.
*/
const NO_CONTEXT = 'none';
/**
* The OG context provider.
*
* @var \Drupal\og\OgContextInterface
*/
protected $ogContext;
/**
* Constructs a new UserCacheContextBase class.
*
* @param \Drupal\og\OgContextInterface $og_context
* The OG context provider.
*/
public function __construct(OgContextInterface $og_context) {
$this->ogContext = $og_context;
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return new TranslatableMarkup('OG active group');
}
/**
* {@inheritdoc}
*/
public function getContext() {
// Do not provide a cache context if there is no group in the current
// context.
$group = $this->ogContext
->getGroup();
if (empty($group)) {
return self::NO_CONTEXT;
}
// Compose a cache context string that consists of the entity type ID and
// the entity ID of the active group.
return implode(':', [
$group
->getEntityTypeId(),
$group
->id(),
]);
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
return new CacheableMetadata();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgGroupContextCacheContext:: |
protected | property | The OG context provider. | |
OgGroupContextCacheContext:: |
public | function |
Gets the cacheability metadata for the context. Overrides CacheContextInterface:: |
|
OgGroupContextCacheContext:: |
public | function |
Returns the string representation of the cache context. Overrides CacheContextInterface:: |
|
OgGroupContextCacheContext:: |
public static | function |
Returns the label of the cache context. Overrides CacheContextInterface:: |
|
OgGroupContextCacheContext:: |
constant | The string to return when no context is found. | ||
OgGroupContextCacheContext:: |
public | function | Constructs a new UserCacheContextBase class. |