Non-SQL entity types in Views, entity references and entity query

Task type
Project
Changed

Context

Entity types whose storage is not SQL (External Entities, ContentEntityNullStorage such as contact_message) are allowed by the entity API, but Views, entity reference relationships and entity query mostly assume every content entity type has SQL tables. On PHP 8.5 the assumption started to show: code that used to build Views data under a NULL table name, which PHP silently turned into '', now raises "Using null as an array offset is deprecated".

Found on the contrib site, where the drupal.org issue, module and release history entities are External Entities referenced from nodes and terms:

Both are patched locally (patches/drupal-views-field-data-null-base-table.diff, patches/registration-3628463-mr203-3.4.9.diff).

Root issues

1. Table names live in two places that can disagree

The entity type definition stores base_table / data_table, but the SQL storage decides them: DefaultTableMapping falls back to <id> and <id>_field_data when the definition omits them. Code that asks the definition gets NULL for an entity type that has real tables, and the same NULL for one that has none at all, so "no table name" cannot be told apart from "default table name".

2. Views assumes SQL storage

Views data, handlers and the query plugin read entity tables without checking whether the storage has any.

3. The PHP 8.5 symptoms, fixed one at a time

Each was fixed with a guard where the code met a NULL table; none addressed the causes above.

References and entity query

  • An entity reference field can only target an entity type with an ID key (EntityReferenceItem throws "has no ID key and cannot be targeted"), so contact_message cannot be referenced while External Entities can. Core ships no referenceable entity type without tables, which is why #3641172 needed a test module.
  • Views relationships to a non-SQL target cannot be made to work in core: entity_reverse joins SQL tables, and a view of external entities has to use xntt_views and its own query plugin. #3641172 drops those relationships; they were never usable.
  • Entity query on SQL storage reads the definition too (#3174342). View on Drupal.org #2875033Optimize joins and table selection in SQL entity query implementation Status: Closed (fixed) (fixed in 11.4) asked about external entities in comment #39 without an answer.

Further findings

Found in a wider search of drupal.org; status taken from search results, not yet checked on each issue page.

Views

Entity query

Entity references

Ideas

  • Views should check for SqlEntityStorageInterface and take every table name from the table mapping (#2079019), which getPrimaryTableName() (#2960037) would reduce to one call.
  • Contrib modules that build Views data for every entity type should do the same; Registration is one, there are likely others.
  • Entity query conditions across a reference (field.entity.x) should hand off to the target storage's query when it is not SQL, rather than joining tables (#2424791).

TODO