function AccessResult::inheritCacheability

Same name in other branches
  1. 9 core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::inheritCacheability()
  2. 8.9.x core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::inheritCacheability()
  3. 10 core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::inheritCacheability()

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.

Parameters

\Drupal\Core\Access\AccessResultInterface $other: The other access result, whose cacheability (if any) to inherit.

Return value

$this

File

core/lib/Drupal/Core/Access/AccessResult.php, line 408

Class

AccessResult
Value object for passing an access result with cacheability metadata.

Namespace

Drupal\Core\Access

Code

public function inheritCacheability(AccessResultInterface $other) {
    $this->addCacheableDependency($other);
    if ($other instanceof CacheableDependencyInterface) {
        if ($this->getCacheMaxAge() !== 0 && $other->getCacheMaxAge() !== 0) {
            $this->setCacheMaxAge(Cache::mergeMaxAges($this->getCacheMaxAge(), $other->getCacheMaxAge()));
        }
        else {
            $this->setCacheMaxAge($other->getCacheMaxAge());
        }
    }
    return $this;
}

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