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)

CommentFileSizeAuthor
#5 3183359-5.patch4.11 KBm.stenta
#2 3183359-2.patch3.15 KBm.stenta

Issue fork drupal-3183359

Command icon 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

m.stenta created an issue. See original summary.

m.stenta’s picture

StatusFileSize
new3.15 KB

Attached 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 FieldStorageDefinitionInterface API surface constitute a breaking change, or affect downstream code?

m.stenta’s picture

Issue summary: View changes

Cross-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!)

m.stenta’s picture

Status: Active » Needs work
Issue tags: +Needs tests
m.stenta’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new4.11 KB

Added some basic tests (simply copied the tests for setCustomStorage() and hasCustomStorage()).

m.stenta’s picture

Status: Needs review » Closed (won't fix)
Related issues: +#2346347: Finalize API for creating, overriding, and altering code-defined bundle fields

I found another way to achieve the same result using the Entity API module's BundleFieldDefinition class.

https://git.drupalcode.org/project/entity/-/blob/8.x-1.x/src/BundleField...

This class extends BaseFieldDefinition and simply overrides the isBaseField() method to return FALSE. This causes core's allowsSharedTableStorage() to also return FALSE, 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".

tonytheferg’s picture

Status: Closed (won't fix) » Active

I 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?

🤷

tonytheferg’s picture

The 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!

zeeshan_khan made their first commit to this issue’s fork.

zeeshan_khan’s picture

@tonytheferg - agree with you
+1 for the feature.
FYI patch works for 9.5.x as well

Version: 9.2.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hitchshock’s picture

+1 Nice feature for a custom entities

rbrownell’s picture

Status: Active » Reviewed & tested by the community

Tested and reviewed patch on #5. Works great. Hopefully we can get this into core soon.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs subsystem maintainer review

There is no MR here.

hitchshock’s picture

Status: Needs work » Reviewed & tested by the community

Provided the MR for the patch file.
Back status to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work

There should be test coverage to demonstrate that setting a dedicated table on the base field actually results in a dedicated table being created.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.