Problem/Motivation
After #3537962: EntityFieldManager::getFieldDefinitions() per-bundle caching can be expensive, moderation state field definitions may be built with an incorrect target bundle. This change exposed a latent bug in ContentModerationHooks::entityBundleFieldInfo() in which a base field definition is returned without first being cloned, allowing the target bundle to be mutated through a shared reference.
Steps to reproduce
- Create two moderated bundles.
- Inspect the field definitions for both bundles.
- One of the
moderation_statedefinitions will have the wrong target bundle.
Proposed resolution
Return a cloned base field definition in ContentModerationHooks::entityBundleFieldInfo().
Remarks
- It's more performant to fix this in each hook implementation, albeit less reliable than deep cloning
$base_field_definitionsin the entity field manager prior to each invocation. - Since the entity field manager already sets the target bundle for each definition, that same step can be omitted in the content moderation module's hook implementation.
- I chose Major priority for this issue since it's a "significant [...] developer-facing bug with no workaround."
Issue fork drupal-3611529
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:
- 3611529-moderation-state-field
changes, plain diff MR !16344
Comments
Comment #2
clayfreemanComment #4
clayfreemanReady for review.
Comment #5
acbramley commentedCouple small comments but otherwise this looks good to go.
Comment #6
clayfreemanMade the requested test change; ready for review again.
Comment #7
acbramley commentedThanks! Good to go now
Comment #8
longwaveThanks for the explanation of how the bug occurs, that makes sense to me and was helpful when reviewing.
Committed and pushed 28d2798c5cd to main and e179aaeca3b to 11.x and bced52eee4d to 11.4.x. Thanks!