fix: preserve source locale data when copying locale from localized subfields with a non-localized parent field(array, block) #12601
+107
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
When copying locale data, only the toLocaleData(target locale) got the value from the copyFrom locale data, while all other locales besides target locale was lost. It happens for array/block fields that were not localized themselves but contained localized subfields. Details in issue #12536
Why?
After digging into this, I found the issue was in the
mergeLocaleActions
logic withinpackages/payload/src/fields/hooks/beforeChange/promise.ts
. The beforeChange hook couldn't access complete locale data becauseupdateByIDOperation
only queries current target locale data (locale: req.locale), so siblingDocWithLocales passed to beforeChange hooks lacks source locale data(non-target locales's data). When mergeLocaleActions tries to preserve existing locale values fromsiblingDocWithLocales
, it can't find the source locale data and sets it to undefined, effectively deleting it.How?
My solution is to fetch complete locale data (locale: 'all') in the copy operation and passing it through request context to the beforeChange hooks, ensuring all locale values are preserved during the merge process.
The fix ensures that when copying locale data, the system has access to the complete picture of all locale values, not just the target locale, preventing accidental data loss.
The changes are targeted only to Copy Locale Operation and the beforeChange hook, and it only affect when the new added context exists, so it won't make affect to other operations.
Current int tests check if the toLocaleData value received from fromLocaleData, however it overlooked the case that source locale data is lost. So I added int test to check if the non-target locale stay the same.
I believe there’s a better way to fix this, but for now this is the best solution I can think of.
Fixes #12536