Problem/Motivation
Opening this as an issue to track and solve an issue that @symioquine discovered: bundle fields defined via hook_farm_entity_bundle_field_info() cannot be written to via JSON:API.
@symbioquine tracked this to a Drupal core issue related to the core hook_entity_bundle_field_info() hook: #3045509: EntityFieldManager key/value field map gets out of sync, doesn't recognise bundle fields
I (@mstenta) confirmed that this issue affects farmOS's hook as well (because it uses that core hook behind the scenes via the Entity API module). See my comment here for more info: https://www.drupal.org/project/drupal/issues/3045509#comment-14726416
Steps to reproduce
- Create a custom module that adds a bundle field to an existing asset/log type via
hook_farm_entity_bundle_field_info()(see: https://farmos.org/development/module/fields/#bundle-fields) - Attempt to write to this new field via JSON:API, and observe that it does not work.
Proposed resolution
The upstream issue is tricky to solve in core (see the core issue for details why). So, I propose we solve this in farmOS as a first step, and if Drupal core solves it we can adopt that in the future.
We can solve this by implementing hook_modules_installed() and hook_modules_uninstalled() to update field maps when modules that implement hook_farm_entity_bundle_field_info() are installed/uninstalled.
We will also need an update hook to fix bundle fields in modules that have already been installed on existing deployments.
Remaining tasks
- Implement
hook_modules_installed()to add farmOS bundle fields to the field map. - Implement
hook_modules_uninstalled()to remove farmOS bundle fields from the field map. - Implement
hook_post_update_NAME()to add farmOS bundle fields to the field map on existing deployments. - Automated tests.
User interface changes
None.
API changes
Fix the ability to write to farmOS bundle fields via the API.
Data model changes
None.
Comments
Comment #2
m.stentaComment #3
m.stentaComment #4
m.stentaOpened a pull request to fix this: https://github.com/farmOS/farmOS/pull/583
Comment #5
m.stentaComment #6
m.stentaComment #7
m.stentaComment #8
m.stentaNew PR with a simplified approach that leverages a core patch from #3129179: Provide some way to rebuild the persistent bundle field map: https://github.com/farmOS/farmOS/pull/854
Comment #11
paul121 commentedComment #13
m.stentaUpdate: Drupal core has fixed the root cause of this in #3045509: EntityFieldManager key/value field map gets out of sync, doesn't recognise bundle fields
This hasn't been merged yet.
I've opened a draft PR to prepare for undoing our fix: https://github.com/farmOS/farmOS/pull/1074