diff --git a/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php b/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php index 58aede1920..9861d3830b 100644 --- a/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php +++ b/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php @@ -194,7 +194,6 @@ public function getEntityTypes() { * {@inheritdoc} */ public function installEntityType(EntityTypeInterface $entity_type) { - $this->clearCachedDefinitions(); $this->entityTypeListener->onEntityTypeCreate($entity_type); } @@ -203,7 +202,6 @@ public function installEntityType(EntityTypeInterface $entity_type) { */ public function updateEntityType(EntityTypeInterface $entity_type) { $original = $this->getEntityType($entity_type->id()); - $this->clearCachedDefinitions(); $this->entityTypeListener->onEntityTypeUpdate($entity_type, $original); } @@ -211,7 +209,6 @@ public function updateEntityType(EntityTypeInterface $entity_type) { * {@inheritdoc} */ public function uninstallEntityType(EntityTypeInterface $entity_type) { - $this->clearCachedDefinitions(); $this->entityTypeListener->onEntityTypeDelete($entity_type); } @@ -227,7 +224,6 @@ public function updateFieldableEntityType(EntityTypeInterface $entity_type, arra $original_field_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type->id()); $this->entityTypeListener->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox); - $this->clearCachedDefinitions(); } /** @@ -243,7 +239,6 @@ public function installFieldStorageDefinition($name, $entity_type_id, $provider, ->setProvider($provider) ->setTargetBundle(NULL); } - $this->clearCachedDefinitions(); $this->fieldStorageDefinitionListener->onFieldStorageDefinitionCreate($storage_definition); } @@ -260,7 +255,6 @@ public function getFieldStorageDefinition($name, $entity_type_id) { */ public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { $original = $this->getFieldStorageDefinition($storage_definition->getName(), $storage_definition->getTargetEntityTypeId()); - $this->clearCachedDefinitions(); $this->fieldStorageDefinitionListener->onFieldStorageDefinitionUpdate($storage_definition, $original); } @@ -268,7 +262,6 @@ public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $st * {@inheritdoc} */ public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { - $this->clearCachedDefinitions(); $this->fieldStorageDefinitionListener->onFieldStorageDefinitionDelete($storage_definition); } @@ -387,12 +380,4 @@ protected function requiresEntityDataMigration(EntityTypeInterface $entity_type, return ($storage instanceof EntityStorageSchemaInterface) && $storage->requiresEntityDataMigration($entity_type, $original); } - /** - * Clears necessary caches to apply entity/field definition updates. - */ - protected function clearCachedDefinitions() { - $this->entityTypeManager->clearCachedDefinitions(); - $this->entityFieldManager->clearCachedFieldDefinitions(); - } - } diff --git a/core/lib/Drupal/Core/Entity/EntityTypeListener.php b/core/lib/Drupal/Core/Entity/EntityTypeListener.php index 4bf05404d9..c68f59b4cf 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeListener.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeListener.php @@ -75,6 +75,7 @@ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) { $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id)); } + $this->clearCachedDefinitions(); $this->eventDispatcher->dispatch(EntityTypeEvents::CREATE, new EntityTypeEvent($entity_type)); } @@ -95,6 +96,7 @@ public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeI } $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type); + $this->clearCachedDefinitions(); $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original)); } @@ -117,6 +119,7 @@ public function onEntityTypeDelete(EntityTypeInterface $entity_type) { } $this->entityLastInstalledSchemaRepository->deleteLastInstalledDefinition($entity_type_id); + $this->clearCachedDefinitions(); $this->eventDispatcher->dispatch(EntityTypeEvents::DELETE, new EntityTypeEvent($entity_type)); } @@ -139,9 +142,18 @@ public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, En if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) { $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions); } + $this->clearCachedDefinitions(); $this->eventDispatcher->dispatch(EntityTypeEvents::UPDATE, new EntityTypeEvent($entity_type, $original)); } } + /** + * Clears necessary caches before dispatching entity/field definition events. + */ + protected function clearCachedDefinitions() { + $this->entityTypeManager->clearCachedDefinitions(); + $this->entityFieldManager->clearCachedFieldDefinitions(); + } + }