diff --git a/core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php b/core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php index aa917c8..bd4b985 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php @@ -96,6 +96,7 @@ public function getAllBundleInfo() { // First look for entity types that act as bundles for others, load them // and add them as bundles. if ($bundle_entity_type = $entity_type->getBundleEntityType()) { + $this->bundleInfo[$type] = []; foreach ($this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple() as $entity) { $this->bundleInfo[$type][$entity->id()]['label'] = $entity->label(); } diff --git a/core/modules/language/src/Form/ContentLanguageSettingsForm.php b/core/modules/language/src/Form/ContentLanguageSettingsForm.php index 619d944..34a2225 100644 --- a/core/modules/language/src/Form/ContentLanguageSettingsForm.php +++ b/core/modules/language/src/Form/ContentLanguageSettingsForm.php @@ -60,7 +60,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $bundles = $this->entityManager->getAllBundleInfo(); $language_configuration = []; foreach ($entity_types as $entity_type_id => $entity_type) { - if (!$entity_type instanceof ContentEntityTypeInterface || !$entity_type->hasKey('langcode') || !isset($bundles[$entity_type_id])) { + if (!$entity_type instanceof ContentEntityTypeInterface || !$entity_type->hasKey('langcode') || empty($bundles[$entity_type_id])) { continue; } $labels[$entity_type_id] = $entity_type->getLabel() ?: $entity_type_id; diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php index d4e0442..b4f4569 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php @@ -7,6 +7,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeBundleInfo; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -145,6 +146,49 @@ protected function setUpEntityTypeDefinitions($definitions = []) { } /** + * Sets up entity bundles to be tested. + * + * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $bundle_entities + */ + protected function setUpEntityBundleTypes($bundle_entities = []) { + foreach ($bundle_entities as $bundle_entity) { + $bundle_type = $bundle_entity->reveal()->getBundleEntityType(); + if ($bundle_type) { + if ($bundle_type == 'nut_type_bundle') { + // When bundles exist. + $almond = $this->prophesize(EntityInterface::class); + $almond->id()->willReturn('almond'); + $almond->label()->willReturn('Almond'); + + $walnut = $this->prophesize(EntityInterface::class); + $walnut->id()->willReturn('walnut'); + $walnut->label()->willReturn('Walnut'); + + $entity_bundles = [ + $almond->reveal(), + $walnut->reveal() + ]; + + $entityStorage = $this->prophesize(EntityStorageInterface::class); + $entityStorage->loadMultiple()->willReturn($entity_bundles); + $this->entityTypeManager->getStorage($bundle_type) + ->willReturn($entityStorage->reveal()); + + } + elseif ($bundle_type == 'bread_type_bundle') { + // When no bundles exist. + $entity_bundles = []; + $entityStorage = $this->prophesize(EntityStorageInterface::class); + $entityStorage->loadMultiple()->willReturn($entity_bundles); + $this->entityTypeManager->getStorage($bundle_type) + ->willReturn($entityStorage->reveal()); + + } + } + } + } + + /** * Tests the clearCachedBundles() method. * * @covers ::clearCachedBundles @@ -224,10 +268,23 @@ public function testGetAllBundleInfo() { $banana->getLabel()->willReturn('Banana'); $banana->getBundleEntityType()->willReturn(NULL); - $this->setUpEntityTypeDefinitions([ + $nut = $this->prophesize(EntityTypeInterface::class); + $nut->getLabel()->willReturn('Nut Type'); + $nut->getBundleEntityType()->willReturn('nut_type_bundle'); + + $bread = $this->prophesize(EntityTypeInterface::class); + $bread->getLabel()->willReturn('Bread Type'); + $bread->getBundleEntityType()->willReturn('bread_type_bundle'); + + $entity_list = [ 'apple' => $apple, 'banana' => $banana, - ]); + 'nut' => $nut, + 'bread' => $bread, + ]; + + $this->setUpEntityTypeDefinitions($entity_list); + $this->setUpEntityBundleTypes($entity_list); $this->cacheBackend->get('entity_bundle_info:en')->willReturn(FALSE); $this->cacheBackend->set('entity_bundle_info:en', Argument::any(), Cache::PERMANENT, ['entity_types', 'entity_bundles']) @@ -253,10 +310,22 @@ public function testGetAllBundleInfo() { 'label' => 'Banana', ], ], + 'nut' => [ + 'almond' => [ + 'label' => 'Almond', + ], + 'walnut' => [ + 'label' => 'Walnut', + ], + ], + 'bread' => [], ]; + + // Check before cache return. $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo(); $this->assertSame($expected, $bundle_info); + // Check cache return. $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo(); $this->assertSame($expected, $bundle_info);