Issue #3586375: Install target_entities storage when DER is enabled later.
Description
Fixes Install `target_entities` field storage when Dy... (#3586375) • Unassigned • 1.0.0-beta5
Sites that already have ai_context installed can fatal when dynamic_entity_reference is enabled later. The context item listing and other entity loads fail with:
Table 'db.ai_context_item__target_entities' doesn't exist
Root cause
target_entities is not defined in AiContextItem::baseFieldDefinitions(). It is added at runtime via DynamicEntityReferenceHooks::entityBaseFieldInfo() when dynamic_entity_reference is enabled, with provider set to dynamic_entity_reference. That makes the field visible to the Entity API, but Drupal does not automatically create the dedicated field storage table on existing sites.
Resolution
- Add
AiContextDynamicEntityReferenceSetupservice to idempotently installtarget_entitiesfield storage viaEntityDefinitionUpdateManager::installFieldStorageDefinition(). - Add
hook_modules_installed()so storage is created when DER is enabled on an existing site (UI, Drush, or config import). - Add
ai_context_update_10015()to repair sites that already enabled DER before this fix. - Update PHPDoc in
DynamicEntityReferenceHooksso it no longer implies core always creates storage automatically on existing sites.
Scope
- Covers DER enabled after initial
ai_contextinstall. - Covers
ai_contextenabled when DER is already present. - Covers config-sync module installs (
$is_syncing = TRUE). - No user-facing config or API changes.
Testing instructions
Automated
From the project root:
cd web/modules/contrib/ai_context
./lint.sh
ddev phpunit web/modules/contrib/ai_context/tests/src/Kernel/AiContextDynamicEntityReferenceInstallTest.phpExpected: lint passes; 3 kernel tests pass.
Manual — reproduce and verify fix
- Install and enable
ai_contextwithoutdynamic_entity_reference. - Create at least one context item.
- Enable
dynamic_entity_reference(/admin/modulesordrush pm:enable dynamic_entity_reference). - Visit
/admin/config/ai/context/items— listing loads without error. - Confirm table exists, e.g.
drush sql:query "SHOW TABLES LIKE 'ai_context_item__target_entities'".
Manual — update hook repair path
On a site that already has DER enabled but is missing storage (or simulate by uninstalling the field storage definition):
drush updatedb -y
drush crExpected: update 10015 reports storage installed (or already installed on re-run). Context item listing loads.
Checklist
- I have updated the MR title to use format:
Issue #1234: My issue title. - I have updated the MR description to include:
Closes #1234 - I have performed a self-review of my own code
- I have added or updated tests, or explained in the description why this change is not covered by tests
- I have updated documentation for any new or changed functionality
- I have written testing instructions and verified them locally
- I have noted any required post-merge steps (config imports, cache rebuilds, manual changes)
- This MR contains no breaking API or hook changes, or they are explicitly documented in the description
Post-merge: Sites upgrading from a release without this fix should run drush updatedb after updating ai_context. Sites that already enabled DER and hit the fatal may need one drush updatedb (or re-enable DER on a dev copy first to confirm).
Documentation: PHPDoc in DynamicEntityReferenceHooks.php updated. No end-user docs required.
Note
Check the one that best describes your usage, or leave all unchecked if AI was not significantly used.
-
AI Assisted Code
Mainly written by a human; AI used for autocomplete or partial generation under full human supervision. -
AI Generated Code
Mainly generated by AI, reviewed and approved by a human before this MR was created. -
Vibe Coded
Generated by AI and only functionally reviewed before this MR was created.
Closes #3586375