I have a custom entity type (eg: myentitytype), and I would like to add a new base field to it via hook_entity_base_field_info(). As I understand it, single value fields (with a cardinality of 1) will create new columns in the myentitytype_field_data table, but multi-value fields will create a new table.

My question: is it possible to force a single-value field to be stored in its own database table, rather than adding columns to the myentity_field_data table?

  • Can you not add it as config entity fields as opposed to entity base fields? I think they all have their individual tables. (Not sure about the multi / single value fields)
    – baikho
    Nov 18 '20 at 13:21
  • 1
    Yes that's true - a config entity field would put it in a separate table. I was specifically curious about where the logic for base fields makes this decision, and if there were any way to specify. Thanks!
    – m.stenta
    Nov 18 '20 at 13:39
  • For anyone curious why: I am considering adding a Geofield as a base field, which has 9 columns. It may result in sparse data if its in the shared table. Other logic will depend on this field, which is why I am considering making it a base field.
    – m.stenta
    Nov 18 '20 at 13:41

Seems the following class is responsible for determining a separate field storage table:


It returns a boolean based on the following 4 conditions:

public function allowsSharedTableStorage(FieldStorageDefinitionInterface $storage_definition) {
  return !$storage_definition->hasCustomStorage() && $storage_definition->isBaseField() && !$storage_definition->isMultiple() && !$storage_definition->isDeleted();

The only feasible solution would be to implement your own Storage handler and flag the basefield with "custom_storage" => TRUE

  • Thanks this is exactly the logic I was looking for! :-)
    – m.stenta
    Nov 18 '20 at 13:38
  • 3
    FYI: As an alternative solution, I created a proposal for Drupal core that would add a new hasDedicatedStorage() method to base field definitions, which would allow developers to explicitly specify that they want dedicated table storage for their single-value base field. drupal.org/project/drupal/issues/3183359
    – m.stenta
    Nov 19 '20 at 1:16

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.