trait CacheableResponseTrait

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

Provides an implementation of CacheableResponseInterface.

Hierarchy

See also

\Drupal\Core\Cache\CacheableResponseInterface

6 files declare their use of CacheableResponseTrait
CacheableResourceResponse.php in core/modules/jsonapi/src/CacheableResourceResponse.php
CacheableSecuredRedirectResponse.php in core/lib/Drupal/Core/Routing/CacheableSecuredRedirectResponse.php
CacheableTestResponse.php in core/modules/system/tests/modules/early_rendering_controller_test/src/CacheableTestResponse.php
HtmlResponse.php in core/lib/Drupal/Core/Render/HtmlResponse.php
ResourceResponse.php in core/modules/rest/src/ResourceResponse.php

... See full list

File

core/lib/Drupal/Core/Cache/CacheableResponseTrait.php, line 10

Namespace

Drupal\Core\Cache
View source
trait CacheableResponseTrait {
    
    /**
     * The cacheability metadata.
     *
     * @var \Drupal\Core\Cache\CacheableMetadata
     */
    protected $cacheabilityMetadata;
    
    /**
     * {@inheritdoc}
     */
    public function addCacheableDependency($dependency) {
        // A trait doesn't have a constructor, so initialize the cacheability
        // metadata if that hasn't happened yet.
        if (!isset($this->cacheabilityMetadata)) {
            $this->cacheabilityMetadata = new CacheableMetadata();
        }
        $this->cacheabilityMetadata = $this->cacheabilityMetadata
            ->merge(CacheableMetadata::createFromObject($dependency));
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata() {
        // A trait doesn't have a constructor, so initialize the cacheability
        // metadata if that hasn't happened yet.
        if (!isset($this->cacheabilityMetadata)) {
            $this->cacheabilityMetadata = new CacheableMetadata();
        }
        return $this->cacheabilityMetadata;
    }

}

Members

Title Sort descending Modifiers Object type Summary
CacheableResponseTrait::$cacheabilityMetadata protected property The cacheability metadata.
CacheableResponseTrait::addCacheableDependency public function
CacheableResponseTrait::getCacheableMetadata public function

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