5

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.

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?

3
  • 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
    Commented Nov 18, 2020 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
    Commented Nov 18, 2020 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
    Commented Nov 18, 2020 at 13:41

1 Answer 1

5

\Drupal\Core\Entity\Sql\DefaultTableMapping is responsible for determining a separate field storage table; its allowsSharedTableStorage() method returns a Boolean value basing on four 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 use "custom_storage" => TRUE.

2
  • Thanks this is exactly the logic I was looking for! :-)
    – m.stenta
    Commented Nov 18, 2020 at 13:38
  • 4
    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
    Commented Nov 19, 2020 at 1:16

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.