I am a Drupal 7 avid user and I am just getting into Drupal 8 development for work. The project we are currently working on involves creating about 3 different custom entities (not nodes, users, etc). I am not sure why, but Drupal 8 documentation is very sparse compared to the Drupal 7 documentation, and there doesn't appear to be a clear answer on how to create a custom entity, attach fields to it, and then create bundles of that entity with additional fields per bundle.

I have tried using drupal generate:entity:content, drupal generate:entity:config and drupal generate:entity:bundle, but none of them seem to do what I am trying to do.

I am trying to specify a new custom entity type, and expose it to the Drupal UI so I can define the fields for the base entity, as well as the fields attached to each entity type, similar to how nodes can be accessed using Structure > Content Types > Article/Basic Page.

In Drupal 7, you used to be able to use entity construction toolkit, but since that's not available in Drupal 8 anymore, and the documentation doesn't give a straight answer, I am stuck trying to figure out how to do it on my own.

Does Drupal Console just not support a way to create a custom entity, attach fields to it, and define bundles of that entity with their own additional fields?

If I have to go into the scaffolding and manually make changes in code to do some of the stuff, that's fine, but is there a resource somewhere that explains to me how to do that? Because I haven't been able to find it in the Drupal 8 documentation.

  • Drupal console will generate an entity, but last I checked you had to edit it to support bundles. After that it works like core Drupal. – Kevin Jan 21 at 19:45
  • 1
    I might be wrong but I always thought the generate functions were just for generating code (think it uses github.com/Chi-teck/drupal-code-generator). So it'll create you a content entity class, the associated bundle config entity class, but not the config entity that represents an instance of a bundle, for example. I don't think there's an interface for generating the code for the base fields (could be wrong again though). Similarly it will generate the code for field plugins (type, widget, formatter), but not the config which creates and attaches a field – Clive Jan 21 at 19:48
  • 2
    In D8+ there are content and config entities. Here we are talking about content entities. Latest versions of drush comes with a generate command, so you can run drush generate content-entity I've been used drush generated entities in several projects with no issue, just considerate the generated code as a scaffold, so you need to customize it latter but is a good starting point. – Akalam Jan 22 at 7:58
  • So I found out that Clive was correct in that base fields cannot be defined via the UI, and you have to explicitly set them up in the scaffolding to add base fields. Once you do that though, you can seemingly create the bundles in the UI, as well as create bundle specific fields. I am just having issues with the scaffolding (with and without custom field definitions) breaking my drupal core installation once I enable the module. – Ex0r Jan 22 at 20:08
  • For the code part generate:entity:content is absolutely the correct generator, and bundles are then "clickdev". The Base fields are defined under the src/Entity/[your_entity].php > Entity::baseFieldDefinitions(); Also Note, if you look at your database, base fields are all on the main table which (for me at least) only pertains to a small set of unique identifiers. Where "Entity" abstracts all the joining you would ever need to do, base fields would only seem to limit you. Then also to update them you have to write the update as a hook_update_*. – Dylan Jan 22 at 21:25

Your Answer

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

Browse other questions tagged or ask your own question.