diff --git a/core/lib/Drupal/Core/Config/ConfigBase.php b/core/lib/Drupal/Core/Config/ConfigBase.php index 51801b1..a57c9c0 100644 --- a/core/lib/Drupal/Core/Config/ConfigBase.php +++ b/core/lib/Drupal/Core/Config/ConfigBase.php @@ -10,7 +10,6 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; -use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\MutableCacheableDependencyInterface; use Drupal\Core\Cache\MutableCacheableDependencyTrait; use \Drupal\Core\DependencyInjection\DependencySerializationTrait; @@ -30,7 +29,7 @@ * @see \Drupal\Core\Config\Config * @see \Drupal\Core\Theme\ThemeSettings */ -abstract class ConfigBase implements CacheableDependencyInterface, MutableCacheableDependencyInterface { +abstract class ConfigBase implements MutableCacheableDependencyInterface { use DependencySerializationTrait; use MutableCacheableDependencyTrait; diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 2a2de2f..67b106b 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -186,12 +186,21 @@ protected function doLoadMultiple(array $ids = NULL) { // Load all of the configuration entities. $records = array(); foreach ($this->configFactory->loadMultiple($names) as $config) { - $records[$config->get($this->idKey)] = $this->overrideFree ? $config->getOriginal(NULL, FALSE) : $config->get(); + $id = $config->get($this->idKey); + $records[$id] = $this->overrideFree ? $config->getOriginal(NULL, FALSE) : $config->get(); // Add cacheability metadata to the record. - $records[$config->get($this->idKey)]['cacheContexts'] = $config->getCacheContexts(); - $records[$config->get($this->idKey)]['cacheTags'] = $config->getCacheTags(); - $records[$config->get($this->idKey)]['cacheMaxAge'] = $config->getCacheMaxAge(); + $records[$id]['cacheContexts'] = $config->getCacheContexts(); + $records[$id]['cacheTags'] = $config->getCacheTags(); + $records[$id]['cacheMaxAge'] = $config->getCacheMaxAge(); + + // Remove the self-referring cache tag that is present on Config objects, + // a ConfigEntity doesn't need this since it will be dynamically generated + // in EntityInterface::getCacheTagsForInvalidation(). The cache tags are + // merged during rendering, and having fewer tags available improves + // performance. + $key = array_search('config:' . $config->getName(), $records[$id]['cacheTags']); + unset($records[$id]['cacheTags'][$key]); } return $this->mapFromStorageRecords($records); } diff --git a/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideTest.php b/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideTest.php index 9495a87..cc6e024 100644 --- a/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideTest.php +++ b/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideTest.php @@ -52,14 +52,22 @@ public function testConfigOverride() { $theme = $config->get('default'); $this->assertEqual('pirate', $theme); - // Check that the cacheability properties are correct. + // Check that the cacheability metadata is correct. $this->assertEqual(['pirate_day'], $config->getCacheContexts()); $this->assertEqual(['config:system.theme', 'pirate-day-tag'], $config->getCacheTags()); $this->assertEqual(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $config->getCacheMaxAge()); + } + + /** + * Tests if config overrides set cacheability metadata on config entities. + */ + public function testConfigEntityOverride() { + // It's pirate day today! + $GLOBALS['it_is_pirate_day'] = TRUE; // Load the User login block and check that its cacheability metadata is - // overridden correctly. This ensures that the metadata is correctly applied - // to config entities. + // overridden correctly. This verifies that the metadata is correctly + // applied to config entities. /** @var EntityManagerInterface $entity_manager */ $entity_manager = $this->container->get('entity.manager'); $block = $entity_manager->getStorage('block')->load('call_to_action'); @@ -67,7 +75,7 @@ public function testConfigOverride() { // Check that our call to action message is appealing to filibusters. $this->assertEqual($block->label(), 'Draw yer cutlasses!'); - // Check that the cacheability metadata properties are correct. + // Check that the cacheability metadata is correct. $this->assertEqual(['pirate_day'], $block->getCacheContexts()); $this->assertEqual(['config:block.block.call_to_action', 'pirate-day-tag'], $block->getCacheTags()); $this->assertEqual(PirateDayCacheContext::PIRATE_DAY_MAX_AGE, $block->getCacheMaxAge()); diff --git a/core/modules/config/tests/config_override_test/config_override_test.services.yml b/core/modules/config/tests/config_override_test/config_override_test.services.yml index 886b7fd..4dba664 100644 --- a/core/modules/config/tests/config_override_test/config_override_test.services.yml +++ b/core/modules/config/tests/config_override_test/config_override_test.services.yml @@ -11,7 +11,7 @@ services: class: Drupal\config_override_test\ConfigOverriderLowPriority tags: - { name: config.factory.override, priority: -100 } - config_override_test.pirate_day_cache_context_overrider: - class: Drupal\config_override_test\PirateDayCacheContextConfigOverride + config_override_test.pirate_day_cache_context_override: + class: Drupal\config_override_test\PirateDayCacheabilityMetadataConfigOverride tags: - { name: config.factory.override } diff --git a/core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php b/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php similarity index 89% rename from core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php rename to core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php index e884dcb..86779d7 100644 --- a/core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php +++ b/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\config_override_test\PirateDayCacheContextConfigOverride. + * Contains \Drupal\config_override_test\PirateDayCacheabilityMetadataConfigOverride. */ namespace Drupal\config_override_test; @@ -14,7 +14,7 @@ /** * Test implementation of a config override that provides cacheability metadata. */ -class PirateDayCacheContextConfigOverride implements ConfigFactoryOverrideInterface { +class PirateDayCacheabilityMetadataConfigOverride implements ConfigFactoryOverrideInterface { /** * {@inheritdoc} diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php index c4413eb..ae1aa1d 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Config\Entity { +use Drupal\Core\Cache\Cache; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Language\Language; @@ -611,6 +612,18 @@ public function testSaveChangedUuid() { array('', array('id' => 'foo')), array('id', 'foo'), ))); + $config_object->expects($this->exactly(1)) + ->method('getCacheContexts') + ->will($this->returnValue([])); + $config_object->expects($this->exactly(1)) + ->method('getCacheTags') + ->will($this->returnValue(['config:foo'])); + $config_object->expects($this->exactly(1)) + ->method('getCacheMaxAge') + ->will($this->returnValue(Cache::PERMANENT)); + $config_object->expects($this->exactly(1)) + ->method('getName') + ->will($this->returnValue('foo')); $this->cacheTagsInvalidator->expects($this->never()) ->method('invalidateTags'); @@ -664,6 +677,18 @@ public function testLoad() { array('', array('id' => 'foo')), array('id', 'foo'), ))); + $config_object->expects($this->exactly(1)) + ->method('getCacheContexts') + ->will($this->returnValue([])); + $config_object->expects($this->exactly(1)) + ->method('getCacheTags') + ->will($this->returnValue(['config:foo'])); + $config_object->expects($this->exactly(1)) + ->method('getCacheMaxAge') + ->will($this->returnValue(Cache::PERMANENT)); + $config_object->expects($this->exactly(1)) + ->method('getName') + ->will($this->returnValue('foo')); $this->configFactory->expects($this->once()) ->method('loadMultiple') @@ -694,6 +719,19 @@ public function testLoadMultipleAll() { array('', array('id' => 'foo')), array('id', 'foo'), ))); + $foo_config_object->expects($this->exactly(1)) + ->method('getCacheContexts') + ->will($this->returnValue([])); + $foo_config_object->expects($this->exactly(1)) + ->method('getCacheTags') + ->will($this->returnValue(['config:foo'])); + $foo_config_object->expects($this->exactly(1)) + ->method('getCacheMaxAge') + ->will($this->returnValue(Cache::PERMANENT)); + $foo_config_object->expects($this->exactly(1)) + ->method('getName') + ->will($this->returnValue('foo')); + $bar_config_object = $this->getMockBuilder('Drupal\Core\Config\Config') ->disableOriginalConstructor() ->getMock(); @@ -703,6 +741,18 @@ public function testLoadMultipleAll() { array('', array('id' => 'bar')), array('id', 'bar'), ))); + $bar_config_object->expects($this->exactly(1)) + ->method('getCacheContexts') + ->will($this->returnValue([])); + $bar_config_object->expects($this->exactly(1)) + ->method('getCacheTags') + ->will($this->returnValue(['config:bar'])); + $bar_config_object->expects($this->exactly(1)) + ->method('getCacheMaxAge') + ->will($this->returnValue(Cache::PERMANENT)); + $bar_config_object->expects($this->exactly(1)) + ->method('getName') + ->will($this->returnValue('foo')); $this->configFactory->expects($this->once()) ->method('listAll') @@ -742,6 +792,18 @@ public function testLoadMultipleIds() { array('', array('id' => 'foo')), array('id', 'foo'), ))); + $config_object->expects($this->exactly(1)) + ->method('getCacheContexts') + ->will($this->returnValue([])); + $config_object->expects($this->exactly(1)) + ->method('getCacheTags') + ->will($this->returnValue(['config:foo'])); + $config_object->expects($this->exactly(1)) + ->method('getCacheMaxAge') + ->will($this->returnValue(Cache::PERMANENT)); + $config_object->expects($this->exactly(1)) + ->method('getName') + ->will($this->returnValue('foo')); $this->configFactory->expects($this->once()) ->method('loadMultiple')