diff --git a/core/lib/Drupal/Core/Config/TypedConfigManager.php b/core/lib/Drupal/Core/Config/TypedConfigManager.php index 12f1849..e31eab6 100644 --- a/core/lib/Drupal/Core/Config/TypedConfigManager.php +++ b/core/lib/Drupal/Core/Config/TypedConfigManager.php @@ -43,6 +43,15 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI protected $definitions; /** + * The array of plugin definitions that correspond to entity types. + * + * This array is keyed by entity type ID, not by plugin ID. + * + * @var array + */ + protected $entityTypeDefinitions = []; + + /** * Creates a new typed configuration manager. * * @param \Drupal\Core\Config\StorageInterface $configStorage @@ -160,14 +169,17 @@ public function getDefinition($base_plugin_id, $exception_on_invalid = TRUE, $is * {@inheritdoc} */ public function getDefinitionForEntityType($entity_type_id) { - $id = 'undefined'; - foreach ($this->getDefinitions() as $definition_id => $definition) { - if (isset($definition['config_entity_type']) && $definition['config_entity_type'] === $entity_type_id) { - $id = $definition_id; - break; + if (!isset($this->entityTypeDefinitions[$entity_type_id])) { + $id = 'undefined'; + foreach ($this->getDefinitions() as $definition_id => $definition) { + if (isset($definition['config_entity_type']) && $definition['config_entity_type'] === $entity_type_id) { + $id = $definition_id; + break; + } } + $this->entityTypeDefinitions[$entity_type_id] = $this->getDefinition($id); } - return $this->getDefinition($id); + return $this->entityTypeDefinitions[$entity_type_id]; } /** @@ -175,6 +187,7 @@ public function getDefinitionForEntityType($entity_type_id) { */ public function clearCachedDefinitions() { $this->schemaStorage->reset(); + $this->entityTypeDefinitions = []; parent::clearCachedDefinitions(); } diff --git a/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php b/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php index 71a091d..9fedbf4 100644 --- a/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php @@ -39,12 +39,17 @@ public function testGetDefinitionForEntityType($entity_type_id, $expected) { ->disableOriginalConstructor() ->setMethods(['getDefinitions']) ->getMock(); - $manager->expects($this->any()) + // Once for the initial loop and once from within getDefintion(). + $manager->expects($this->exactly(2)) ->method('getDefinitions') ->willReturn($definitions); $definition = $manager->getDefinitionForEntityType($entity_type_id); $this->assertSame($expected, $definition); + + // Ensure this doesn't loop over getDefinitions() twice. + $definition = $manager->getDefinitionForEntityType($entity_type_id); + $this->assertSame($expected, $definition); } /**