class AccountPermissionsCacheContext in Drupal 9
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php \Drupal\Core\Cache\Context\AccountPermissionsCacheContext
Defines the AccountPermissionsCacheContext service, for "per permission" caching.
Cache context ID: 'user.permissions'.
Hierarchy
- class \Drupal\Core\Cache\Context\UserCacheContextBase
- class \Drupal\Core\Cache\Context\AccountPermissionsCacheContext implements CacheContextInterface
Expanded class hierarchy of AccountPermissionsCacheContext
1 string reference to 'AccountPermissionsCacheContext'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses AccountPermissionsCacheContext
File
- core/
lib/ Drupal/ Core/ Cache/ Context/ AccountPermissionsCacheContext.php, line 14
Namespace
Drupal\Core\Cache\ContextView source
class AccountPermissionsCacheContext extends UserCacheContextBase implements CacheContextInterface {
/**
* The permissions hash generator.
*
* @var \Drupal\Core\Session\PermissionsHashGeneratorInterface
*/
protected $permissionsHashGenerator;
/**
* Constructs a new UserCacheContext service.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The current user.
* @param \Drupal\Core\Session\PermissionsHashGeneratorInterface $permissions_hash_generator
* The permissions hash generator.
*/
public function __construct(AccountInterface $user, PermissionsHashGeneratorInterface $permissions_hash_generator) {
$this->user = $user;
$this->permissionsHashGenerator = $permissions_hash_generator;
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("Account's permissions");
}
/**
* {@inheritdoc}
*/
public function getContext() {
return $this->permissionsHashGenerator
->generate($this->user);
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
$cacheable_metadata = new CacheableMetadata();
// The permissions hash changes when:
// - a user is updated to have different roles;
$tags = [
'user:' . $this->user
->id(),
];
// - a role is updated to have different permissions.
foreach ($this->user
->getRoles() as $rid) {
$tags[] = "config:user.role.{$rid}";
}
return $cacheable_metadata
->setCacheTags($tags);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AccountPermissionsCacheContext:: |
protected | property | The permissions hash generator. | |
AccountPermissionsCacheContext:: |
public | function |
Gets the cacheability metadata for the context. Overrides CacheContextInterface:: |
|
AccountPermissionsCacheContext:: |
public | function |
Returns the string representation of the cache context. Overrides CacheContextInterface:: |
|
AccountPermissionsCacheContext:: |
public static | function |
Returns the label of the cache context. Overrides CacheContextInterface:: |
|
AccountPermissionsCacheContext:: |
public | function |
Constructs a new UserCacheContext service. Overrides UserCacheContextBase:: |
|
UserCacheContextBase:: |
protected | property | The account object. |