Skip to content

fix: preserve source locale data when copying locale from localized subfields with a non-localized parent field(array, block) #12601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anyuj
Copy link
Contributor

@anyuj anyuj commented May 29, 2025

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 within packages/payload/src/fields/hooks/beforeChange/promise.ts. The beforeChange hook couldn't access complete locale data because updateByIDOperation 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 from siblingDocWithLocales, 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

@anyuj anyuj force-pushed the fix--copy-to-locale-deal-with-array-and-block-fields-properly branch from 594d0ca to 8a10c1c Compare May 29, 2025 10:04
@anyuj anyuj marked this pull request as draft May 29, 2025 10:18
@anyuj anyuj force-pushed the fix--copy-to-locale-deal-with-array-and-block-fields-properly branch from 8a10c1c to 94aba64 Compare May 29, 2025 11:45
@anyuj anyuj marked this pull request as ready for review May 29, 2025 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When copying or saving to a locale, the localized fields in arrays or blocks of other locales lose their values
1 participant