Problem/Motivation
Right now Drupal core has logic to decide if base field data should be stored in the shared entity field data table (eg: node_field_data), or in a dedicated table (eg: node__myfield). This logic is contained in the DefaultTableMapping::allowsSharedTableStorage() method:
/**
* Checks whether the given field can be stored in a shared table.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
* The field storage definition.
*
* @return bool
* TRUE if the field can be stored in a shared table, FALSE otherwise.
*/
public function allowsSharedTableStorage(FieldStorageDefinitionInterface $storage_definition) {
return !$storage_definition->hasCustomStorage() && $storage_definition->isBaseField() && !$storage_definition->isMultiple() && !$storage_definition->isDeleted();
}
Thus, a field will be saved to the shared table storage if a) it does not have custom storage, AND b) it is a base field, AND c) it is not a multi-value field, AND d) it is not deleted.
This means that single-value base fields will ALWAYS be stored in the shared table. While this is beneficial to reducing the number of table JOINs required, it is not always desirable.
If a field is not required, it's possible to end up with a lot of empty cells / sparse data in the shared table, which could otherwise be avoided by a dedicated table. This is compounded by fields that add a lot of columns (like Geofields which add 9).
And more generally, it would be nice to provide the option to developers to explicitly declare that some fields should be stored in dedicated tables. Even though Drupal tries to abstract a lot of these low-level decisions, it is still useful to expose some of them to the developer so they can make the right choice for their particular application.
Proposed resolution
I propose we add two new methods to the BaseFieldDefinition class: setDedicatedTable() and hasDedicatedTable()
The latter can then be added as an additional condition in the allowsSharedTableStorage() method logic (described above).
Thus, a developer defining a new base field could do something like this to specify that the field data should be saved in a dedicated table:
$fields['myfield'] = BaseFieldDefinition::create('string')
->setLabel(t('My field'))
->setDedicatedTable(TRUE);
The method names setDedicatedTable() and hasDedicatedTable() were chosen to match the existing pattern set by setCustomStorage() and hasCustomStorage() methods, but open to other ideas if you think those don't perfectly convey their purpose.
Remaining tasks
... (tbd)
User interface changes
None.
API changes
This would change the API surface of FieldStorageDefinitionInterface to have an additional method: hasDedicatedTable() (returns boolean true/false).
Data model changes
None.
Release notes snippet
... (tbd)
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3183359-5.patch | 4.11 KB | m.stenta |
| #2 | 3183359-2.patch | 3.15 KB | m.stenta |
Issue fork drupal-3183359
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
m.stentaAttached is a patch that works as described in the issue description, just to get the conversation started. I tested it with a Geofield base field definition, and it successfully created a separate table (in the same way that it would if it were not a base field).
The patch does not have tests yet.
Is this worth pursuing further? Are there other considerations to take into account? Does the change to the
FieldStorageDefinitionInterfaceAPI surface constitute a breaking change, or affect downstream code?Comment #3
m.stentaCross-referencing this related stackexchange question, which ultimately led to the creation of this issue: https://drupal.stackexchange.com/questions/298250/force-a-single-value-b... (thanks to @kim.kennof and @johnzzon in Drupal #support Slack for pointing me in the right direction!)
Comment #4
m.stentaComment #5
m.stentaAdded some basic tests (simply copied the tests for
setCustomStorage()andhasCustomStorage()).Comment #6
m.stentaI found another way to achieve the same result using the Entity API module's
BundleFieldDefinitionclass.https://git.drupalcode.org/project/entity/-/blob/8.x-1.x/src/BundleField...
This class extends
BaseFieldDefinitionand simply overrides theisBaseField()method to returnFALSE. This causes core'sallowsSharedTableStorage()to also returnFALSE, and the field data is thus stored in a dedicated table.And according to the docblock for this class, it sounds like core may eventually adopt a similar class. See #2346347: Finalize API for creating, overriding, and altering code-defined bundle fields.
All of these considerations. combined with the fact that this issue's patch would change the API surface of
FieldStorageDefinitionInterface, makes me think it's better to close this as "won't fix".Comment #7
tonytheferg commentedI think a method for letting a basefield have it's own storage would be better than having to override the class?
I don't see any modules that use bundleFieldDefinitions::create for base fields on entities, so it's unclear what the supported approach is?
🤷
Comment #8
tonytheferg commentedThe patch works well from what I can tell and seems far less convoluted than overriding the class and fussing with defining your own storage.
+1 for this feature!
Comment #10
zeeshan_khan commented@tonytheferg - agree with you
+1 for the feature.
FYI patch works for 9.5.x as well
Comment #12
hitchshock+1 Nice feature for a custom entities
Comment #13
rbrownellTested and reviewed patch on #5. Works great. Hopefully we can get this into core soon.
Comment #14
catchThere is no MR here.
Comment #18
hitchshockProvided the MR for the patch file.
Back status to RTBC.
Comment #19
catchThere should be test coverage to demonstrate that setting a dedicated table on the base field actually results in a dedicated table being created.