AccountPermissionsCacheContext.php

Same filename and directory in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php
  2. 8.9.x core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php
  3. 10 core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php

Namespace

Drupal\Core\Cache\Context

File

core/lib/Drupal/Core/Cache/Context/AccountPermissionsCacheContext.php

View source
<?php

namespace Drupal\Core\Cache\Context;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\PermissionsHashGeneratorInterface;

/**
 * Defines the AccountPermissionsCacheContext service, for "per permission" caching.
 *
 * Cache context ID: 'user.permissions'.
 */
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() {
        return $this->permissionsHashGenerator
            ->getCacheableMetadata($this->user);
    }

}

Classes

Title Deprecated Summary
AccountPermissionsCacheContext Defines the AccountPermissionsCacheContext service, for "per permission" caching.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.