/** * {@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; }
/** * @covers ::merge * @dataProvider providerTestMerge * * This only tests at a high level, because it reuses existing logic. Detailed * tests exist for the existing logic: * * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags() * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges() * @see \Drupal\Tests\Core\Cache\CacheContextsTest */ public function testMerge(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) { $cache_contexts_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')->disableOriginalConstructor()->getMock(); $container = new ContainerBuilder(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container); $this->assertEquals($expected, $a->merge($b)); }
/** * Merges the values of another bubbleable metadata object with this one. * * @param \Drupal\Core\Cache\CacheableMetadata $other * The other bubbleable metadata object. * * @return static * A new bubbleable metadata object, with the merged data. */ public function merge(CacheableMetadata $other) { $result = parent::merge($other); if ($other instanceof BubbleableMetadata) { $result->attached = \Drupal::service('renderer')->mergeAttachments($this->attached, $other->attached); $result->postRenderCache = NestedArray::mergeDeep($this->postRenderCache, $other->postRenderCache); } return $result; }
/** * Merges the values of another bubbleable metadata object with this one. * * @param \Drupal\Core\Cache\CacheableMetadata $other * The other bubbleable metadata object. * * @return static * A new bubbleable metadata object, with the merged data. */ public function merge(CacheableMetadata $other) { $result = parent::merge($other); // This is called many times per request, so avoid merging unless absolutely // necessary. if ($other instanceof BubbleableMetadata) { if (empty($this->attachments)) { $result->attachments = $other->attachments; } elseif (empty($other->attachments)) { $result->attachments = $this->attachments; } else { $result->attachments = static::mergeAttachments($this->attachments, $other->attachments); } } return $result; }
/** * {@inheritdoc} */ public function addCacheableDependency($dependency) { $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency)); return $this; }