class PathParentCacheContext

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/Context/PathParentCacheContext.php \Drupal\Core\Cache\Context\PathParentCacheContext
  2. 10 core/lib/Drupal/Core/Cache/Context/PathParentCacheContext.php \Drupal\Core\Cache\Context\PathParentCacheContext
  3. 11.x core/lib/Drupal/Core/Cache/Context/PathParentCacheContext.php \Drupal\Core\Cache\Context\PathParentCacheContext

Defines a cache context service for path parents.

Cache context ID: 'url.path.parent'.

This allows for caching based on the path, excluding everything after the last forward slash.

Hierarchy

Expanded class hierarchy of PathParentCacheContext

1 file declares its use of PathParentCacheContext
PathParentCacheContextTest.php in core/tests/Drupal/Tests/Core/Cache/Context/PathParentCacheContextTest.php
1 string reference to 'PathParentCacheContext'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses PathParentCacheContext
cache_context.url.path.parent in core/core.services.yml
Drupal\Core\Cache\Context\PathParentCacheContext

File

core/lib/Drupal/Core/Cache/Context/PathParentCacheContext.php, line 15

Namespace

Drupal\Core\Cache\Context
View source
class PathParentCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {
    
    /**
     * {@inheritdoc}
     */
    public static function getLabel() {
        return t('Parent path');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext() {
        $request = $this->requestStack
            ->getCurrentRequest();
        $path_elements = explode('/', trim($request->getPathInfo(), '/'));
        array_pop($path_elements);
        return implode('/', $path_elements);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata() {
        return new CacheableMetadata();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
PathParentCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
PathParentCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
PathParentCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
RequestStackCacheContextBase::$requestStack protected property The request stack.
RequestStackCacheContextBase::__construct public function Constructs a new RequestStackCacheContextBase class.

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