class UserRolesCacheContext
Same name in other branches
- 9 core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext
- 8.9.x core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext
- 10 core/lib/Drupal/Core/Cache/Context/UserRolesCacheContext.php \Drupal\Core\Cache\Context\UserRolesCacheContext
Defines the UserRolesCacheContext service, for "per role" caching.
Only use this cache context when checking explicitly for certain roles. Use user.permissions for anything that checks permissions.
Cache context ID: 'user.roles' (to vary by all roles of the current user). Calculated cache context ID: 'user.roles:%role', e.g. 'user.roles:anonymous' (to vary by the presence/absence of a specific role).
Hierarchy
- class \Drupal\Core\Cache\Context\UserCacheContextBase
- class \Drupal\Core\Cache\Context\UserRolesCacheContext extends \Drupal\Core\Cache\Context\UserCacheContextBase implements \Drupal\Core\Cache\Context\CalculatedCacheContextInterface
Expanded class hierarchy of UserRolesCacheContext
1 file declares its use of UserRolesCacheContext
- UserRolesCacheContextTest.php in core/
tests/ Drupal/ Tests/ Core/ Cache/ Context/ UserRolesCacheContextTest.php
File
-
core/
lib/ Drupal/ Core/ Cache/ Context/ UserRolesCacheContext.php, line 17
Namespace
Drupal\Core\Cache\ContextView source
class UserRolesCacheContext extends UserCacheContextBase implements CalculatedCacheContextInterface {
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("User's roles");
}
/**
* {@inheritdoc}
*/
public function getContext($role = NULL) {
// User 1 does not actually have any special behavior for roles; this is
// added as additional security and backwards compatibility protection for
// SA-CORE-2015-002.
// @todo Remove in Drupal 9.0.0.
if ($this->user
->id() == 1) {
return 'is-super-user';
}
if ($role === NULL) {
return implode(',', $this->user
->getRoles());
}
else {
return in_array($role, $this->user
->getRoles()) ? 'true' : 'false';
}
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($role = NULL) {
return (new CacheableMetadata())->setCacheTags([
'user:' . $this->user
->id(),
]);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
UserCacheContextBase::$user | protected | property | The account object. | ||
UserCacheContextBase::__construct | public | function | Constructs a new UserCacheContextBase class. | 1 | |
UserRolesCacheContext::getCacheableMetadata | public | function | Overrides CalculatedCacheContextInterface::getCacheableMetadata | ||
UserRolesCacheContext::getContext | public | function | Overrides CalculatedCacheContextInterface::getContext | ||
UserRolesCacheContext::getLabel | public static | function | Overrides CalculatedCacheContextInterface::getLabel |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.