0

The docs I've found so far tell how to add bundle-specific fields in either hook_entity_bundle_field_info() or in bundleFieldDefinitions() in my content entity class. While those work it seems cumbersome since they put all bundle field definitions in the same place. Since bundle classes extend a content entity class, it seems like it would be a lot cleaner to define additional fields in the bundle class they belong to.

Is that possible?

I've tried both implementing bundleFieldDefinitions() and overriding baseFieldDefinitions() in my bundle class, but it appears they never get called there.

5
  • Is your module available publically? Trying to implement bundle class but not able to succeed Commented Jul 15, 2022 at 17:36
  • This is currently a work project. I do plan on creating an example module and making that public when I get this all figured out. Commented Jul 16, 2022 at 18:54
  • Cool, I have created a module if you can have a look , pls let me know what went wrong. I used the different hook in . module as you suggested in different thread but still no success. git.drupalcode.org/project/omdb_api Commented Jul 17, 2022 at 3:22
  • 1
    Your .module file looks like it's still implementing hook_entity_bundle_info_alter(). See api.drupal.org/api/drupal/… and define your bundles like this $bundles['my_type']['my_bundle'] = [ 'label' => t('My bundle'), 'class' => MyBundle::class ]; Commented Jul 18, 2022 at 11:51
  • For your bundle fields, see api.drupal.org/api/drupal/… Commented Jul 18, 2022 at 11:58

1 Answer 1

1

I was able to do this by implementing bundleFieldDefinitions in both my content entity class and my bundle class. The bundle class defines its fields, then the entity class bundleFieldDefinitions() determines the bundle and calls the appropriate bundle class bundleFieldDefinitions()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.