2

I've created entities and bundles through Entity Construction Kit UI, and now I'm trying to re-create everything with code, in a module. I managed to generate entity with Drupal console, added fields, and everything works well.

The problem is when I try to create bundles for an entity, with different fields. In ECK, I can create an entity, then bundles, and add different fields to each bundle. However, I can't understand how to do it in the code, and after reading tons of documentation, it feels like bundles are only configuration entities and can't have fields.

So my questions are :

  • Is there something I don't understand ?
  • How can I mimic ECK "bundles with fields" in code ? My goal is to create profiles, like the profile module can do for users, but referenced from an other entity, and with specific behavior, so I'd prefer create my own entities.
3
  • I finally found drupal.stackexchange.com/questions/204261/… that explains it's not managed by Drupal, and ECK is using tricks to make this work.
    – Grubshka
    Commented Aug 14, 2017 at 17:16
  • At the moment define entity types in code and "bundles with fields" in /config/install/*.yaml. It was planned from the beginning to make bundles with fields possible in code, but work is still in progress, see this issue drupal.org/node/2346347
    – 4uk4
    Commented Aug 14, 2017 at 17:55
  • @4k4 Thanks for your answer and for the link, this answers my question.
    – Grubshka
    Commented Aug 15, 2017 at 19:23

1 Answer 1

4

Yes, the bundle entities themself are not fieldable.

But bundle config entities in the end only exist to manage bundles for the content entity type, and as far as content entities are concerned, bundles are basically just a string (and a bit of metadata like a title). It's even possible to have bundles based on plugins or other things.

Per-bundle fields (usually configurable fields defined only in config, but it would be possible to define them in code as well, currently not trivial) are then attached to a content entity type + specific bundle. Fields actually consist of two definitions: field storage, which is tied to the entity type and defines name and storage-level settings, and the field, which is for a bundle and has bundle-level settings, label and so on. Base field definitions defined in the baseFieldDefinitions() method are exposed as both the field storage and field definition.

Haven't yet read it in detail, but http://www.daggerhart.com/drupal-8-custom-entities-bundles/ is a recent (as in, ~1h old) extensive resource on how to build entity types with bundles.

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.