AccessResult
abstract class AccessResult implements AccessResultInterface, RefinableCacheableDependencyInterface (View source)
Value object for passing an access result with cacheability metadata.
The access result itself — excluding the cacheability metadata — is immutable. There are subclasses for each of the three possible access results themselves:
Traits
Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.
Trait for \Drupal\Core\Cache\CacheableDependencyInterface.
Properties
| protected string[] | $cacheContexts | Cache contexts. |
from CacheableDependencyTrait |
| protected string[] | $cacheTags | Cache tags. |
from CacheableDependencyTrait |
| protected int | $cacheMaxAge | Cache max-age. |
from CacheableDependencyTrait |
Methods
Sets cacheability; useful for value object constructors.
The cache tags associated with this object.
The cache contexts associated with this object.
The maximum age for which this object may be cached.
Creates an AccessResultInterface object with isNeutral() === TRUE.
Creates an AccessResultInterface object with isAllowed() === TRUE.
Creates an AccessResultInterface object with isForbidden() === TRUE.
Creates an allowed or neutral access result.
Creates a forbidden or neutral access result.
Creates an allowed access result if the permission is present, neutral otherwise.
Creates an allowed access result if the permissions are present, neutral otherwise.
Checks whether this access result indicates access is explicitly allowed.
Checks whether this access result indicates access is explicitly forbidden.
Checks whether this access result indicates access is not yet determined.
Resets cache contexts (to the empty array).
Resets cache tags (to the empty array).
Sets the maximum age for which this access result may be cached.
Convenience method, adds the "user.permissions" cache context.
Convenience method, adds the "user" cache context.
Inherits the cacheability of the other access result, if any.
Details
protected $this
setCacheability(CacheableDependencyInterface $cacheability)
Sets cacheability; useful for value object constructors.
string[]
getCacheTags()
The cache tags associated with this object.
When this object is modified, these cache tags will be invalidated.
string[]
getCacheContexts()
The cache contexts associated with this object.
These identify a specific variation/representation of the object.
Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.
int
getCacheMaxAge()
The maximum age for which this object may be cached.
addCacheableDependency($other_object)
{@inheritdoc}
addCacheContexts(array $cache_contexts)
{@inheritdoc}
addCacheTags(array $cache_tags)
{@inheritdoc}
mergeCacheMaxAge($max_age)
{@inheritdoc}
static AccessResultNeutral
neutral(string|null $reason = NULL)
Creates an AccessResultInterface object with isNeutral() === TRUE.
static AccessResultAllowed
allowed()
Creates an AccessResultInterface object with isAllowed() === TRUE.
static AccessResultForbidden
forbidden(string|null $reason = NULL)
Creates an AccessResultInterface object with isForbidden() === TRUE.
static AccessResult
allowedIf(bool $condition)
Creates an allowed or neutral access result.
static AccessResult
forbiddenIf(bool $condition, string|null $reason = NULL)
Creates a forbidden or neutral access result.
static AccessResult
allowedIfHasPermission(AccountInterface $account, string $permission)
Creates an allowed access result if the permission is present, neutral otherwise.
Checks the permission and adds a 'user.permissions' cache context.
static AccessResult
allowedIfHasPermissions(AccountInterface $account, array $permissions, string $conjunction = 'AND')
Creates an allowed access result if the permissions are present, neutral otherwise.
Checks the permission and adds a 'user.permissions' cache contexts.
bool
isAllowed()
Checks whether this access result indicates access is explicitly allowed.
bool
isForbidden()
Checks whether this access result indicates access is explicitly forbidden.
This is a kill switch — both orIf() and andIf() will result in isForbidden() if either results are isForbidden().
bool
isNeutral()
Checks whether this access result indicates access is not yet determined.
$this
resetCacheContexts()
Resets cache contexts (to the empty array).
$this
resetCacheTags()
Resets cache tags (to the empty array).
$this
setCacheMaxAge(int $max_age)
Sets the maximum age for which this access result may be cached.
$this
cachePerPermissions()
Convenience method, adds the "user.permissions" cache context.
$this
cachePerUser()
Convenience method, adds the "user" cache context.
AccessResultInterface
orIf(AccessResultInterface $other)
Combine this access result with another using OR.
When ORing two access results, the result is:
- isForbidden() in either ⇒ isForbidden()
- otherwise if isAllowed() in either ⇒ isAllowed()
- otherwise both must be isNeutral() ⇒ isNeutral()
Truth table:
AccessResultInterface
andIf(AccessResultInterface $other)
Combine this access result with another using AND.
When ANDing two access results, the result is:
- isForbidden() in either ⇒ isForbidden()
- otherwise, if isAllowed() in both ⇒ isAllowed()
- otherwise, one of them is isNeutral() ⇒ isNeutral()
Truth table:
$this
inheritCacheability(AccessResultInterface $other)
Inherits the cacheability of the other access result, if any.
This method differs from addCacheableDependency() in how it handles max-age, because it is designed to inherit the cacheability of the second operand in the andIf() and orIf() operations. There, the situation "allowed, max-age=0 OR allowed, max-age=1000" needs to yield max-age 1000 as the end result.