W3cubDocs

/Drupal 8

public function CacheableResponseTrait::addCacheableDependency

public CacheableResponseTrait::addCacheableDependency($dependency)

Adds a dependency on an object: merges its cacheability metadata.

For instance, when a response depends on some configuration, an entity, or an access result, we must make sure their cacheability metadata is present on the response. This method makes doing that simple.

Parameters

\Drupal\Core\Cache\CacheableDependencyInterface|mixed $dependency: The dependency. If the object implements CacheableDependencyInterface, then its cacheability metadata will be used. Otherwise, the passed in object must be assumed to be uncacheable, so max-age 0 is set.

Return value

$this

Overrides CacheableResponseInterface::addCacheableDependency

See also

\Drupal\Core\Cache\CacheableMetadata::createFromObject()

File

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

Class

CacheableResponseTrait
Provides an implementation of CacheableResponseInterface.

Namespace

Drupal\Core\Cache

Code

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Cache!CacheableResponseTrait.php/function/CacheableResponseTrait::addCacheableDependency/8.1.x