4

In Drupal 7 there was the start of Per-Bundle Storage (PBS) to try to store field data in a single database table if that made sense and was possible given the nature of the field. The PBS for Drupal 7 module didn't ever become very popular, though and was not ported to Drupal 8.

I understand it's possible to write custom Entity code for Drupal 8 to store data in a single table if you want (e.g. the User entity stores it's fields in users_field_data).

I'm curious if there's an easy solution I've overlooked to store several fields in a single database table.

  • 1
    If you need to store several fields in one table, and you can’t use the base entity table, is that not a good argument for needing a new entity type? Or a reference to an existing type like a paragraph? – Clive Nov 15 '18 at 7:59
  • ... or a custom field type with multiple properties. – 4k4 Nov 15 '18 at 10:09
  • @Clive I'm not sure I follow your answer, perhaps you're confused by my question :) When I create a new entity type in the UI each field becomes it's own table. Is there a way to create an entity type via the UI and use core fields and get the field data into a single table? Or are you suggesting "needing a new entity type" that is created with code that controls the storage of the data? – greggles Nov 15 '18 at 19:08
  • 1
    I’m confused by most things @greggles ;) I think I do understand what you mean though, you want to segment the content type field data so that each node content type is its own table, with only its own fields, right? And yeah, I’m saying the second thing - that the only way I know of to achieve a similar model without getting your hands very dirty, is to get those grouped fields into another “thing” (referenced fieldable entity or custom compound field type as 4k4 mentioned) and attach that to the content type – Clive Nov 15 '18 at 19:40
  • 1
    Oh I see the “via the UI” stipulation now, I missed that before. There isn’t as far as I know; the only way to get the fields into a single table (node_field_data), is to add base fields. But those apply across an entity type, to all bundles – Clive Nov 15 '18 at 19:50
4

Answering my own question based on comments here and my own experimentation. In Drupal 8 the creation of an entity with base fields is simpler than it was in Drupal 7. Here are the steps I followed to create an entity in Drupal 8 with fields in the single table. I'll talk about my situation: a Dog entity.

  1. There's some good general docs on Drupal.org about entities and fields in Drupal 8. This page about defining and using content entity field definitions is particularly useful.
  2. I also read this skvare article about Entities in Drupal 8 which has a lot of great advice.
  3. I started by installing Drupal Console since I didn't yet have it. I was a bit confused about installing console via composer and also having to install the global launcher to have easy access (i.e. without constantly adjusting my PATH).
  4. I ran drupal generate:module and answered the questions. 5 files were created with 58 lines.
  5. I then ran drupal generate:entity:content --module exampledog. I said "no" to bundles for my case. The rest of the answers seemed straightforward. 21 files were created with 1595 lines (A surprisingly large amount).
  6. In the exampledog/src/Entity/Dog.php file I modified the baseFieldDefinitions function to return an additional field. Some examples and documention on field types are in the drupal.org doc page mentioned earlier.

That's it. Revisions seem to work well for these fields. Views integration works well for these fields. The fields are all stored in the base table.

  • Excellent summary. A few notes: Some folks might prefer drush generate module-content-entity Take a look at [hook_entity_base_field_info_alter](hook_entity_base_field_info_alter) for a way to add fields to an entity that you dont control. In general, I prefer base fields to configurable fields as clients and devs can't mess them up as easily as they are not part of the site's config. – moshe weitzman Oct 14 at 22:57
  • Thanks @mosheweitzman! I appreciate the tip on the drush technique and hook_entity_base_info_field_alter and insight on why you prefer base fields. I'll give the drush generate tools a try and perhaps update my answer. – greggles Oct 16 at 17:04

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.