Loading core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +35 −0 Original line number Diff line number Diff line Loading @@ -646,4 +646,39 @@ public function save() { return $return; } /** * Loads one entity in their original form without overrides. * * @param string $id * The ID of the entity to load. * * @return static|null * An entity object. NULL if no matching entity is found. */ public static function loadOverrideFree(string $id): ConfigEntityInterface|null { $entity_type_repository = \Drupal::service('entity_type.repository'); $entity_type_manager = \Drupal::entityTypeManager(); $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class)); assert($storage instanceof ConfigEntityStorageInterface); return $storage->loadOverrideFree($id); } /** * Loads one or more entities in their original form without overrides. * * @param string[]|null $ids * An array of entity IDs, or NULL to load all entities. * * @return static[] * An array of entity objects indexed by their IDs. Returns an empty array * if no matching entities are found. */ public static function loadMultipleOverrideFree(?array $ids = NULL): array { $entity_type_repository = \Drupal::service('entity_type.repository'); $entity_type_manager = \Drupal::entityTypeManager(); $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class)); assert($storage instanceof ConfigEntityStorageInterface); return $storage->loadMultipleOverrideFree($ids); } } core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +73 −0 Original line number Diff line number Diff line Loading @@ -15,6 +15,7 @@ use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeRepositoryInterface; use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ThemeHandlerInterface; Loading Loading @@ -283,6 +284,78 @@ public function testAddDependency(): void { $this->assertEquals(['entity', 'module'], array_keys($dependencies)); } /** * Tests ConfigEntityBase::loadOverrideFree(). * * When called statically on a subclass of ConfigEntityBase. */ public function testLoadOverrideFree(): void { $class_name = get_class($this->entity); $entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class); $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); $storage = $this->createMock(ConfigEntityStorageInterface::class); $storage->expects($this->once()) ->method('loadOverrideFree') ->with($this->id) ->willReturn($this->entity); $this->entityTypeManager = $this->createStub(EntityTypeManagerInterface::class); $this->entityTypeManager ->method('getDefinition') ->willReturn($this->entityType); $this->entityTypeManager ->method('getStorage') ->willReturn($storage); \Drupal::getContainer()->set('entity_type.manager', $this->entityTypeManager); \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); // Call ConfigEntityBase::loadOverrideFree() statically and check that it // returns the mock entity. $this->assertSame($this->entity, $class_name::loadOverrideFree($this->id)); } /** * Tests ConfigEntityBase::loadMultipleOverrideFree(). * * When called statically on a subclass of ConfigEntityBase. */ public function testLoadMultipleOverrideFree(): void { $class_name = get_class($this->entity); $entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class); $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); $storage = $this->createMock(ConfigEntityStorageInterface::class); $storage->expects($this->once()) ->method('loadMultipleOverrideFree') ->with([$this->id]) ->willReturn([$this->id => $this->entity]); $this->entityTypeManager = $this->createStub(EntityTypeManagerInterface::class); $this->entityTypeManager ->method('getDefinition') ->willReturn($this->entityType); $this->entityTypeManager ->method('getStorage') ->willReturn($storage); \Drupal::getContainer()->set('entity_type.manager', $this->entityTypeManager); \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); // Call ConfigEntityBase::loadMultipleOverrideFree() statically and check that it // returns the mock entity. $this->assertSame([$this->id => $this->entity], $class_name::loadMultipleOverrideFree([$this->id])); } /** * Tests calculate dependencies with plugin collections. * Loading Loading
core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +35 −0 Original line number Diff line number Diff line Loading @@ -646,4 +646,39 @@ public function save() { return $return; } /** * Loads one entity in their original form without overrides. * * @param string $id * The ID of the entity to load. * * @return static|null * An entity object. NULL if no matching entity is found. */ public static function loadOverrideFree(string $id): ConfigEntityInterface|null { $entity_type_repository = \Drupal::service('entity_type.repository'); $entity_type_manager = \Drupal::entityTypeManager(); $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class)); assert($storage instanceof ConfigEntityStorageInterface); return $storage->loadOverrideFree($id); } /** * Loads one or more entities in their original form without overrides. * * @param string[]|null $ids * An array of entity IDs, or NULL to load all entities. * * @return static[] * An array of entity objects indexed by their IDs. Returns an empty array * if no matching entities are found. */ public static function loadMultipleOverrideFree(?array $ids = NULL): array { $entity_type_repository = \Drupal::service('entity_type.repository'); $entity_type_manager = \Drupal::entityTypeManager(); $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class)); assert($storage instanceof ConfigEntityStorageInterface); return $storage->loadMultipleOverrideFree($ids); } }
core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +73 −0 Original line number Diff line number Diff line Loading @@ -15,6 +15,7 @@ use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeRepositoryInterface; use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ThemeHandlerInterface; Loading Loading @@ -283,6 +284,78 @@ public function testAddDependency(): void { $this->assertEquals(['entity', 'module'], array_keys($dependencies)); } /** * Tests ConfigEntityBase::loadOverrideFree(). * * When called statically on a subclass of ConfigEntityBase. */ public function testLoadOverrideFree(): void { $class_name = get_class($this->entity); $entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class); $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); $storage = $this->createMock(ConfigEntityStorageInterface::class); $storage->expects($this->once()) ->method('loadOverrideFree') ->with($this->id) ->willReturn($this->entity); $this->entityTypeManager = $this->createStub(EntityTypeManagerInterface::class); $this->entityTypeManager ->method('getDefinition') ->willReturn($this->entityType); $this->entityTypeManager ->method('getStorage') ->willReturn($storage); \Drupal::getContainer()->set('entity_type.manager', $this->entityTypeManager); \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); // Call ConfigEntityBase::loadOverrideFree() statically and check that it // returns the mock entity. $this->assertSame($this->entity, $class_name::loadOverrideFree($this->id)); } /** * Tests ConfigEntityBase::loadMultipleOverrideFree(). * * When called statically on a subclass of ConfigEntityBase. */ public function testLoadMultipleOverrideFree(): void { $class_name = get_class($this->entity); $entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class); $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); $storage = $this->createMock(ConfigEntityStorageInterface::class); $storage->expects($this->once()) ->method('loadMultipleOverrideFree') ->with([$this->id]) ->willReturn([$this->id => $this->entity]); $this->entityTypeManager = $this->createStub(EntityTypeManagerInterface::class); $this->entityTypeManager ->method('getDefinition') ->willReturn($this->entityType); $this->entityTypeManager ->method('getStorage') ->willReturn($storage); \Drupal::getContainer()->set('entity_type.manager', $this->entityTypeManager); \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); // Call ConfigEntityBase::loadMultipleOverrideFree() statically and check that it // returns the mock entity. $this->assertSame([$this->id => $this->entity], $class_name::loadMultipleOverrideFree([$this->id])); } /** * Tests calculate dependencies with plugin collections. * Loading