AccessHelper
Provides helpers for working with access results.
- Full name:
\Drupal\helper\AccessHelper
Properties
variationCache
protected \Drupal\Core\Cache\VariationCacheInterface $variationCache
Methods
__construct
AccessHelper constructor.
public __construct(\Drupal\Core\Cache\VariationCacheInterface $variationCache): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$variationCache |
\Drupal\Core\Cache\VariationCacheInterface | |
| The variation cache around helper's access results bin. |
getCache
Gets a cached access result.
public getCache(array $keys, \Drupal\Core\Session\AccountInterface|null $account = null, \Drupal\Core\Cache\CacheableDependencyInterface|null $initialCacheability = null): \Drupal\Core\Access\AccessResultInterface|null
Results are kept in the helper_access cache bin, varied by their cache contexts, resolved for $account. Results that only vary by permissions are shared by every account with the same permissions, such as all anonymous checks. By default the bin is in memory, so results are kept for the current request only.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$keys |
array | |
| The cache keys, as a list in the same order on every call, starting with | ||
| something unique to the caller, such as METHOD. As with the | ||
| variation cache, only the values and their order matter, not the | ||
| array's own keys. Strings and integers are used as they are. Arrays, | ||
| such as entity values, are sorted by key, recursively, and hashed, so | ||
| the order of their keys doesn't matter. Other scalars and NULL are | ||
| hashed. Objects aren't allowed as keys; pass an ID instead. If an array | ||
| key contains an object, such as an entity value, nothing is cached. | ||
$account |
\Drupal\Core\Session\AccountInterface|null | |
| (optional) The account the result is for. Defaults to the current user. | ||
$initialCacheability |
\Drupal\Core\Cache\CacheableDependencyInterface|null | |
| (optional) Cacheability every result for these keys has, such as cache | ||
| contexts that are always added. Must match the value given to | ||
| setCache(). |
Return Value:
A copy of the cached access result, so callers can add cacheability to it without changing the cache, or NULL if there is none or the keys can't be cached.
setCache
Caches an access result.
public setCache(array $keys, \Drupal\Core\Access\AccessResultInterface $result, \Drupal\Core\Session\AccountInterface|null $account = null, \Drupal\Core\Cache\CacheableDependencyInterface|null $initialCacheability = null): void
Results with max-age 0, or without cacheability metadata, aren't cached, and neither are results whose keys can't be cached.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$keys |
array | |
| The cache keys, as given to getCache(). | ||
$result |
\Drupal\Core\Access\AccessResultInterface | |
| The access result. A copy is cached, so the caller can keep changing it. | ||
$account |
\Drupal\Core\Session\AccountInterface|null | |
| (optional) The account the result is for. Defaults to the current user. | ||
$initialCacheability |
\Drupal\Core\Cache\CacheableDependencyInterface|null | |
| (optional) The initial cacheability, as given to getCache(). |
normalizeCacheKeys
Converts cache keys to the strings the variation cache expects.
protected normalizeCacheKeys(array $keys): string[]|null
Parameters:
| Parameter | Type | Description |
|---|---|---|
$keys |
array | |
| The cache keys, as given to getCache() or setCache(). |
Return Value:
The cache keys as strings, in the same order, or NULL if an array key contains an object, or a key isn't a scalar, NULL or an array.
sortCacheKeyArray
Sorts an array cache key by key, recursively.
protected static sortCacheKeyArray(array& $array): bool
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$array |
array | |
| The array, sorted in place. |
Return Value:
TRUE if the array only contains scalars, NULL and arrays, otherwise FALSE.
merge
Merges two access values.
public static merge(bool|\Drupal\Core\Access\AccessResultInterface $first, bool|\Drupal\Core\Access\AccessResultInterface $second, bool $and = true): bool|\Drupal\Core\Access\AccessResultInterface
Two booleans are combined as booleans. Otherwise both are converted to access results and combined with andIf() or orIf(). As when rendering, any boolean but FALSE counts as allowed.
- This method is static. Parameters:
| Parameter | Type | Description |
|---|---|---|
$first |
bool|\Drupal\Core\Access\AccessResultInterface | |
| The first access value. | ||
$second |
bool|\Drupal\Core\Access\AccessResultInterface | |
| The second access value. | ||
$and |
bool | |
| (optional) TRUE to require both (andIf), FALSE to require either | ||
| (orIf). Defaults to TRUE. |
Return Value:
A boolean if both values are booleans, otherwise an access result.
See Also:
- \Drupal\Core\Render\Renderer::doRender()
Inherited methods
executeAsAccount
Executes a callback as another account.
protected executeAsAccount(\Drupal\Core\Session\AccountInterface|null $account, callable $callback): mixed
While the callback runs, the account is the current user, so cache contexts such as user.permissions resolve for it. The original account is restored afterwards, even if the callback throws an exception. The account isn't switched when it's already the current user.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$account |
\Drupal\Core\Session\AccountInterface|null | |
| The account, or NULL to run the callback as the current user. | ||
$callback |
callable | |
| The callback. |
Return Value:
The callback's return value.
See Also:
- \Drupal\Core\Session\AccessPolicyProcessor::processAccessPolicies()