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.
Comment | File | Size | Author |
---|---|---|---|
#18 | interdiff-3112239-14-18.diff | 584 bytes | Bhanu951 |
#18 | 3112239-18.patch | 11.48 KB | Bhanu951 |
#14 | 3112239-14-10.x.patch | 11.5 KB | Bhanu951 |
Issue fork drupal-3112239
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:
Comments
Comment #5
joachim CreditAttribution: joachim as a volunteer commentedComment #7
andypostIt looks great addition!
Comment #12
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commentedNeeds Re-Roll for 10.1.x branch.
Comment #13
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commented@joachim please update MR #153 target branch to 10.x
Comment #14
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commentedComment #16
joachim CreditAttribution: joachim as a volunteer commented@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.
Comment #17
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commentedAgreed @joachim , I created patch because Gitlab is having issues with MR after recent updates .
Refer https://twitter.com/drupal_infra/status/1613597701215776768
Comment #18
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commentedComment #19
joachim CreditAttribution: joachim as a volunteer commentedWhat is the ViewUI class?? It's not an entity but it's implementing an entity interface!!
Comment #20
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commentedRetesting after latest code changes.
Comment #21
joachim CreditAttribution: joachim as a volunteer commentedNot sure how we deal with this -- mocking static methods is supposed to be bad IIRC: