I have 2 questions about entities inheritance and bundles in Drupal 8.

  • Let's assume that I have a content entity called "foo" (entity type) and "foo" as its bundle, and now I want to create the content entity "bar" with the bundle "foo", and add new properties to it. Is it possibile?
  • Is it possible to use inheritance with content entities, something like the following inheritance (and if so, is that considered to be a bad practice?) :

    class foo extends ContentEntityBase {}

    class bar extends foo{}

    class baz extends bar{}

  • Sounds like you know OO, so I'd put the question back to you - why would that not work? – Clive Oct 1 '15 at 14:17
  • Drupal will "eat" that? I don't want the GUI to freak out. :) If we are now working with class inheritance, so why do we need bundles? – rreiss Oct 1 '15 at 14:18
  • 1
    Sure - unless you make foo the entity controller class, Drupal's never even going to know it exists. Of course you need to manage things smartly inside that base class - if there's entity type-specific logic, make sure it lives in the subclass, not in foo – Clive Oct 1 '15 at 14:23
  • So what is the best practice in D8, should I use entities inheritance or bundles? – rreiss Oct 1 '15 at 14:35
  • I can't really answer that, it depends on what "foo" is and what it needs to do. I don't think I'd be far off saying the advice for Drupal 7 on entity types still holds true here – Clive Oct 1 '15 at 14:36

Drupal Core uses that itself for test entity types:

enter image description here

So yes, this works. However, it only works as far as it is covered by PHP. You can inherit base field definitions for example if you call the parent method and use the return value, see those classes.

Annotations for example are however not inherited, each subclass needs to re-define everything.

There is the plan to provide a default content entity that will a default set of base field definitions and other things that you probably want to have on custom entity types, likely that will live in the 8.x version of the entity.module.

On what you want to use, that depends on your use case. The common use case for bundles is so that users/site builders can create them and add configurable fields. If you need that, use them.

Keep in mind that not everything is generic, various things are still node specific, like the whole revision handling (the storage is generic, but all the user interfaces require a ton of custom code at the moment), the node preview functionality is also custom and other things. We plan to improve that situation in the mentioned entity.module until we can improve it in core.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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