payload
payload copied to clipboard
When copying or saving to a locale, the localized fields in arrays or blocks of other locales lose their values
Describe the Bug
If a collection contains arrays or blocks with localized fields, copying one locale to another or simply saving the current locale causes other locales to lose their values. It seems that localized values are not merged but instead overwritten. I am using MongoDB, so I'm not sure if this behavior is different with relational databases
Link to the code that reproduces this issue
https://github.com/epranka/payload-locales-save-issue
Reproduction Steps
- Clone the repository.
- Run docker compose up and wait until both the app and MongoDB are ready.
- Open http://localhost:3000/admin.
- Create an account.
- Create a new Page. Enter a title, add at least one item in an array or block with filled fields. In the Content tab, add any layout to avoid validation errors. Then, publish the page.
- Click "Copy to locale" and select the "en" locale.
- Switch back to the previous "lt" locale. You should see that the values in the array and block are lost.
- Click "Copy to locale" and select the "lt" locale.
- Switch back to the previous "en" locale. You should now see that the values are lost in that locale instead. Only the last saved locale retains its values.
Which area(s) are affected? (Select all that apply)
Not sure
Environment Info
Binaries:
Node: 20.13.1
npm: 10.5.2
Yarn: N/A
pnpm: 10.11.0
Relevant Packages:
payload: 3.39.1
next: 15.3.0
@payloadcms/db-mongodb: 3.39.1
@payloadcms/email-nodemailer: 3.39.1
@payloadcms/graphql: 3.39.1
@payloadcms/live-preview: 3.39.1
@payloadcms/live-preview-react: 3.39.1
@payloadcms/next/utilities: 3.39.1
@payloadcms/payload-cloud: 3.39.1
@payloadcms/plugin-form-builder: 3.39.1
@payloadcms/plugin-nested-docs: 3.39.1
@payloadcms/plugin-redirects: 3.39.1
@payloadcms/plugin-search: 3.39.1
@payloadcms/plugin-seo: 3.39.1
@payloadcms/richtext-lexical: 3.39.1
@payloadcms/translations: 3.39.1
@payloadcms/ui/shared: 3.39.1
react: 19.1.0
react-dom: 19.1.0
Operating System:
Platform: linux
Arch: x64
Version: #61.1~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Apr 18 12:20:24 UTC
Available memory (MB): 64167
Available CPU cores: 16
I also experienced this issue with postgresql in my local with payload version 3.39.1. I assume this bug appear in 3.36.0 because when i use 3.35.0 is work finely.
I dont know if this changed related but in 3.36.0 release have a change in copyToLocale to avoid duplicate https://github.com/payloadcms/payload/commit/34ead72
Experienced this aswell with MongoDB! For me it seemed to work when I published but not when creating a new document and copying in draft mode.
I also experienced this issue with postgresql in my local with payload version 3.39.1. I assume this bug appear in 3.36.0 because when i use 3.35.0 is work finely.
I dont know if this changed related but in 3.36.0 release have a change in copyToLocale to avoid duplicate 34ead72
The change you mentioned doesn't seem able to cause the bug. I also found the bug only happens to Arrays or Blocks field that itself is not localized enabled, but it has localized nested field inside.
By the way, #4195 This 'Edit multiple locales at once' feature would be a game-changer for any multi-locale website. It can streamline the workflow and solve countless localization headaches. However, not many people aware of this discussion. If you’ve ever wished you could update all your site’s languages in one go, please join me in requesting this feature!
We have noticed this behaviour as well in out project. We have currently 14 Languages, but when "copy to another locale" is used all other languages will be dropped.
We also use MongoDB as our Database.
This is also an issue in Payload v2 (2.32.2) (here with MongoDb). Here it is not directly the "copy to locale" but duplicating items with blocks containing localized fields.
When I tried to track down the issue I came to the same part: the "beforeChange Fields Hook" step in updateById where the locales should be merged.
Here are my findings:
Setup
- "a" collection with blocks containing a localized field (languages: [A, B])
Steps
(A) OK: Update through UI works
- create new item in locale A
- switch to locale B, add new content in localized field and update -> OK: both locales are saved correctly
(B) FAIL: Duplicating through UI drops first locale
- have an item with content for all locales
- duplicate item -> FAIL: locale A is dropped Note: this fails on 2.32.2 mainly because on duplicate each locale gets run through baseBeforeDuplicate causing each locale to have different Ids for nested items than the previous locale; so I implemented a mapping-hack to restore the new nested-item-ids on each locale leading me to the next issue in the beforeChange field hook
(C) FAIL: Update "through code" drops first locale
- create an item via payload.create({data, locale: A, ...})
- update item via payload.update({id, locale: B, fallbackLocale: "none" ....}) -> FAIL: locale A is dropped
I also experienced loss of data after "copy to locale", but the issue was that I have versions enabled for my collection and I just forgot to write a migration for versions data. After migration was added, all works well. Hope it helps someone.
This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.