class OgMembershipStateCacheContext in Organic groups 8
Defines a cache context service, for "membership state" caching.
Cache context ID: 'og_membership_state'
Hierarchy
- class \Drupal\og\Cache\Context\OgMembershipStateCacheContext implements CacheContextInterface
Expanded class hierarchy of OgMembershipStateCacheContext
1 file declares its use of OgMembershipStateCacheContext
- OgMembershipStateCacheContextTest.php in tests/
src/ Unit/ Cache/ Context/ OgMembershipStateCacheContextTest.php
1 string reference to 'OgMembershipStateCacheContext'
1 service uses OgMembershipStateCacheContext
File
- src/
Cache/ Context/ OgMembershipStateCacheContext.php, line 20
Namespace
Drupal\og\Cache\ContextView source
class OgMembershipStateCacheContext implements CacheContextInterface {
/**
* The string to return when no context is found.
*/
const NO_CONTEXT = 'none';
/**
* The membership manager service.
*
* @var \Drupal\og\MembershipManagerInterface
*/
protected $membershipManager;
/**
* The OG context provider.
*
* @var \Drupal\og\OgContextInterface
*/
protected $ogContext;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $user;
/**
* Constructs a new UserCacheContextBase class.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The current user.
* @param \Drupal\og\OgContextInterface $og_context
* The OG context provider.
* @param \Drupal\og\MembershipManagerInterface $membership_manager
* The membership manager service.
*/
public function __construct(AccountInterface $user, OgContextInterface $og_context, MembershipManagerInterface $membership_manager) {
$this->user = $user;
$this->ogContext = $og_context;
$this->membershipManager = $membership_manager;
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return new TranslatableMarkup('OG membership state');
}
/**
* {@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;
}
/** @var \Drupal\og\OgMembershipInterface $membership */
$membership = $this->membershipManager
->getMembership($group, $this->user
->id(), OgMembershipInterface::ALL_STATES);
return $membership ? $membership
->getState() : self::NO_CONTEXT;
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
return new CacheableMetadata();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgMembershipStateCacheContext:: |
protected | property | The membership manager service. | |
OgMembershipStateCacheContext:: |
protected | property | The OG context provider. | |
OgMembershipStateCacheContext:: |
protected | property | The current user. | |
OgMembershipStateCacheContext:: |
public | function |
Gets the cacheability metadata for the context. Overrides CacheContextInterface:: |
|
OgMembershipStateCacheContext:: |
public | function |
Returns the string representation of the cache context. Overrides CacheContextInterface:: |
|
OgMembershipStateCacheContext:: |
public static | function |
Returns the label of the cache context. Overrides CacheContextInterface:: |
|
OgMembershipStateCacheContext:: |
constant | The string to return when no context is found. | ||
OgMembershipStateCacheContext:: |
public | function | Constructs a new UserCacheContextBase class. |