CacheableResponseTrait.php

Same filename in other branches
  1. 8.9.x core/lib/Drupal/Core/Cache/CacheableResponseTrait.php
  2. 10 core/lib/Drupal/Core/Cache/CacheableResponseTrait.php
  3. 11.x core/lib/Drupal/Core/Cache/CacheableResponseTrait.php

Namespace

Drupal\Core\Cache

File

core/lib/Drupal/Core/Cache/CacheableResponseTrait.php

View source
<?php

namespace Drupal\Core\Cache;


/**
 * Provides an implementation of CacheableResponseInterface.
 *
 * @see \Drupal\Core\Cache\CacheableResponseInterface
 */
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;
    }

}

Traits

Title Deprecated Summary
CacheableResponseTrait Provides an implementation of CacheableResponseInterface.

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