/** * Creates a CacheableMetadata object from a depended object. * * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object * The object whose cacheability metadata to retrieve. If it implements * CacheableDependencyInterface, its cacheability metadata will be used, * otherwise, the passed in object must be assumed to be uncacheable, so * max-age 0 is set. * * @return static */ public static function createFromObject($object) { if ($object instanceof CacheableDependencyInterface) { $meta = new static(); $meta->cacheContexts = $object->getCacheContexts(); $meta->cacheTags = $object->getCacheTags(); $meta->cacheMaxAge = $object->getCacheMaxAge(); return $meta; } // Objects that don't implement CacheableDependencyInterface must be assumed // to be uncacheable, so set max-age 0. $meta = new static(); $meta->cacheMaxAge = 0; return $meta; }
/** * Adds a dependency on an object: merges its cacheability metadata. * * @param \Drupal\Core\Cache\CacheableDependencyInterface|object $other_object * 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 $this */ public function addCacheableDependency($other_object) { if ($other_object instanceof CacheableDependencyInterface) { $this->contexts = Cache::mergeContexts($this->contexts, $other_object->getCacheContexts()); $this->tags = Cache::mergeTags($this->tags, $other_object->getCacheTags()); $this->maxAge = Cache::mergeMaxAges($this->maxAge, $other_object->getCacheMaxAge()); } else { $this->maxAge = 0; } return $this; }