function NodeAccessGrantsCacheContext::getCacheableMetadata

Same name and namespace in other branches
  1. 9 core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php \Drupal\node\Cache\NodeAccessGrantsCacheContext::getCacheableMetadata()
  2. 8.9.x core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php \Drupal\node\Cache\NodeAccessGrantsCacheContext::getCacheableMetadata()
  3. 10 core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php \Drupal\node\Cache\NodeAccessGrantsCacheContext::getCacheableMetadata()

Overrides CalculatedCacheContextInterface::getCacheableMetadata

File

core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php, line 84

Class

NodeAccessGrantsCacheContext
Defines the node access view cache context service.

Namespace

Drupal\node\Cache

Code

public function getCacheableMetadata($operation = NULL) {
    $cacheable_metadata = new CacheableMetadata();
    if (!\Drupal::moduleHandler()->hasImplementations('node_grants')) {
        return $cacheable_metadata;
    }
    // The node grants may change if the user is updated. (The max-age is set to
    // zero below, but sites may override this cache context, and change it to a
    // non-zero value. In such cases, this cache tag is needed for correctness.)
    $cacheable_metadata->setCacheTags([
        'user:' . $this->user
            ->id(),
    ]);
    // If the site is using node grants, this cache context can not be
    // optimized.
    return $cacheable_metadata->setCacheMaxAge(0);
}

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