EntityTypeBundleInfo::getAllBundleInfo() should always populate an entity type key with at least an empty array

Created on 22 September 2017, about 7 years ago
Updated 30 January 2023, almost 2 years ago

If an entity has a bundle entity, but there are no bundle entities existing, EntityTypeBundleInfo::getAllBundleInfo() will not have a key in the return array for that entity type.

This means that for example doing this:

    $entity_types = $entity_type_manager->getDefinitions();
    $bundles = $entity_type_bundle_info->getAllBundleInfo();
    foreach ($entity_types as $entity_type_id => $entity_type) {
      foreach ($bundles[$entity_type_id] as $bundle_id => $bundle_info) {
        // ...

will throw notices, and requires a guard clause to check that $bundles has the entity type present.

The documentation for getAllBundleInfo() suggests that it returns data on all entity types:

> Get the bundle info of all entity types.

So for entity types that have no bundles, there should be an empty array.

EDIT:
Steps to recreate:
1) Create module with Drupal Console: drupal gm
2) Generate a controller: drupal gcon
3) Add the following code to the controller function:

public function getHelloEntities {
 $entity_types = $this->entityManager->getDefinitions();
    $bundles = $this->entityManager->getAllBundleInfo();
    $list = '';
    foreach ($entity_types as $entity_type_id => $entity_type) {
      foreach ($bundles[$entity_type_id] as $bundle_id => $bundle_info) {
        $list .= $bundle_id . '::' . implode($bundle_info) . '</ br>';
      }
    }
    return [
      '#type' => 'markup',
      '#markup' => $list
    ];
  }
}

4) Go to "/admin/structure/block/block-content/types" and delete the existing bundle type entity for entity block_content.
5) Go to the controller page "/modulename/getHelloEntities
6) This will now output notices on report page of "Notice: Undefined index: block_content"

🐛 Bug report
Status

Needs work

Version

10.1

Component
Entity 

Last updated 5 minutes ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇬🇧United Kingdom rachel_norfolk UK

    Removing the Novice tag based upon age and the question over whether the change is actually required in #48 and #49

Production build 0.71.5 2024