Sometimes you want an entity type with bundles, but the bundles can be hardcoded rather than be provided by a config entity type, because they're never going to change.

To do that, you need to implement hook_entity_bundle_info().

When it's your own entity type, it feels a bit weird to have to implement a hook, that's away from the entity code. It would be more elegant and clearer if it were possible for an entity class to define its own bundles, like this:

class MyEntity extends ContentEntityBase {

  public static function bundleDefinitions() {
    // Same format as hook_entity_bundle_info(), without the outer key for entity type of course.
  }

}

EntityTypeBundleInfo::getAllBundleInfo() can then call this in the same way that EntityFieldManager::buildBaseFieldDefinitions() does $class::baseFieldDefinitions.

Side benefit: we could probably remove entity_test_create_bundle(), as AFAICT it only serves to set up bundles at the start of tests, not to add them dynamically during a test. That would make the test entity type classes easier to understand too.

Issue fork drupal-3112239

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim created an issue. See original summary.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

joachim’s picture

Status: Active » Needs review

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record, +Needs subsystem maintainer review

It looks great addition!

yogeshmpawar made their first commit to this issue’s fork.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Bhanu951’s picture

Assigned: Unassigned » Bhanu951
Issue tags: +Needs reroll

Needs Re-Roll for 10.1.x branch.

Bhanu951’s picture

@joachim please update MR #153 target branch to 10.x

Bhanu951’s picture

Assigned: Bhanu951 » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
11.5 KB

joachim’s picture

Issue tags: -Needs change record

@Bhanu951 thanks for the reroll, but I think it's best to stick to MRs once an issue is using them, rather than make patches.

Created a new MR on 10.1 and written CR.

Bhanu951’s picture

Agreed @joachim , I created patch because Gitlab is having issues with MR after recent updates .

Refer https://twitter.com/drupal_infra/status/1613597701215776768

Bhanu951’s picture

joachim’s picture

  26     Non-abstract class Drupal\views_ui\ViewUI contains abstract method  
         bundleDefinitions() from interface                                  
         Drupal\Core\Entity\EntityInterface.                                 

What is the ViewUI class?? It's not an entity but it's implementing an entity interface!!

Bhanu951’s picture

Retesting after latest code changes.

joachim’s picture

Status: Needs review » Needs work

Not sure how we deal with this -- mocking static methods is supposed to be bad IIRC:

1) Drupal\Tests\Core\Entity\EntityTypeBundleInfoTest::testGetBundleInfo with data set #0 ('apple', array(array('Apple')))
Error: Class name must be a valid object or a string

/var/www/html/core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php:99
/var/www/html/core/lib/Drupal/Core/Entity/EntityTypeBundleInfo.php:81
/var/www/html/core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php:180
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:673
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:673

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.