You are here

class QueryArgsCacheContext in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php \Drupal\Core\Cache\Context\QueryArgsCacheContext
  2. 9 core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php \Drupal\Core\Cache\Context\QueryArgsCacheContext

Defines the QueryArgsCacheContext service, for "per query args" caching.

Cache context ID: 'url.query_args' (to vary by all query arguments). Calculated cache context ID: 'url.query_args:%key', e.g.'url.query_args:foo' (to vary by the 'foo' query argument).

Hierarchy

Expanded class hierarchy of QueryArgsCacheContext

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

File

core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php, line 14

Namespace

Drupal\Core\Cache\Context
View source
class QueryArgsCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface {

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t('Query arguments');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext($query_arg = NULL) {
    if ($query_arg === NULL) {

      // All arguments requested. Use normalized query string to minimize
      // variations.
      $value = $this->requestStack
        ->getCurrentRequest()
        ->getQueryString();
      return $value !== NULL ? $value : '';
    }
    elseif ($this->requestStack
      ->getCurrentRequest()->query
      ->has($query_arg)) {
      $value = $this->requestStack
        ->getCurrentRequest()->query
        ->all()[$query_arg];
      if (is_array($value)) {
        return http_build_query($value);
      }
      elseif ($value !== '') {
        return $value;
      }
      return '?valueless?';
    }
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($query_arg = NULL) {
    return new CacheableMetadata();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
QueryArgsCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context based on the parameter value. Overrides CalculatedCacheContextInterface::getCacheableMetadata
QueryArgsCacheContext::getContext public function Returns the string representation of the cache context. Overrides CalculatedCacheContextInterface::getContext
QueryArgsCacheContext::getLabel public static function Returns the label of the cache context. Overrides CalculatedCacheContextInterface::getLabel
RequestStackCacheContextBase::$requestStack protected property The request stack.
RequestStackCacheContextBase::__construct public function Constructs a new RequestStackCacheContextBase class.