diff --git a/packages/drizzle/src/transform/read/traverseFields.ts b/packages/drizzle/src/transform/read/traverseFields.ts index 70c81a1da5..4d7012421b 100644 --- a/packages/drizzle/src/transform/read/traverseFields.ts +++ b/packages/drizzle/src/transform/read/traverseFields.ts @@ -177,7 +177,7 @@ export const traverseFields = >({ return arrayResult }, {}) } else { - result[field.name] = fieldData.map((row, i) => { + result[field.name] = fieldData.reduce((acc, row, i) => { if (row._uuid) { row.id = row._uuid delete row._uuid @@ -187,22 +187,35 @@ export const traverseFields = >({ delete row._order } - return traverseFields({ - adapter, - blocks, - config, - dataRef: row, - deletions, - fieldPrefix: '', - fields: field.fields, - numbers, - path: `${sanitizedPath}${field.name}.${i}`, - relationships, - table: row, - texts, - withinArrayOrBlockLocale, - }) - }) + if ( + !withinArrayOrBlockLocale || + (withinArrayOrBlockLocale && withinArrayOrBlockLocale === row._locale) + ) { + if (row._locale) { + delete row._locale + } + + acc.push( + traverseFields({ + adapter, + blocks, + config, + dataRef: row, + deletions, + fieldPrefix: '', + fields: field.fields, + numbers, + path: `${sanitizedPath}${field.name}.${i}`, + relationships, + table: row, + texts, + withinArrayOrBlockLocale, + }), + ) + } + + return acc + }, []) } } diff --git a/test/localization/collections/Blocks/index.ts b/test/localization/collections/Blocks/index.ts index 20faefad0b..10cd389b09 100644 --- a/test/localization/collections/Blocks/index.ts +++ b/test/localization/collections/Blocks/index.ts @@ -29,6 +29,22 @@ export const BlocksCollection: CollectionConfig = { }, ], }, + { + name: 'array', + type: 'array', + fields: [ + { + name: 'link', + type: 'group', + fields: [ + { + name: 'label', + type: 'text', + }, + ], + }, + ], + }, ], }, ], diff --git a/test/localization/collections/NestedArray/index.ts b/test/localization/collections/NestedArray/index.ts index 4f53cf08ee..a6b3b0755d 100644 --- a/test/localization/collections/NestedArray/index.ts +++ b/test/localization/collections/NestedArray/index.ts @@ -2,22 +2,57 @@ import type { CollectionConfig } from 'payload' export const NestedArray: CollectionConfig = { slug: 'nested-arrays', + versions: { + drafts: true, + }, fields: [ { - name: 'arrayWithBlocks', - type: 'array', - localized: true, - fields: [ + type: 'tabs', + tabs: [ { - name: 'blocksWithinArray', - type: 'blocks', - blocks: [ + label: 'My Tab', + fields: [ { - slug: 'someBlock', + name: 'arrayWithBlocks', + type: 'array', + localized: true, fields: [ { - name: 'relationWithinBlock', + name: 'blocksWithinArray', + type: 'blocks', + blocks: [ + { + slug: 'someBlock', + fields: [ + { + name: 'relationWithinBlock', + type: 'relationship', + relationTo: 'localized-posts', + }, + { + name: 'myGroup', + type: 'group', + fields: [ + { + name: 'text', + type: 'text', + }, + ], + }, + ], + }, + ], + }, + ], + }, + { + name: 'arrayWithLocalizedRelation', + type: 'array', + fields: [ + { + name: 'localizedRelation', type: 'relationship', + localized: true, relationTo: 'localized-posts', }, ], @@ -26,17 +61,5 @@ export const NestedArray: CollectionConfig = { }, ], }, - { - name: 'arrayWithLocalizedRelation', - type: 'array', - fields: [ - { - name: 'localizedRelation', - type: 'relationship', - localized: true, - relationTo: 'localized-posts', - }, - ], - }, ], } diff --git a/test/localization/int.spec.ts b/test/localization/int.spec.ts index f7855f938f..1159744201 100644 --- a/test/localization/int.spec.ts +++ b/test/localization/int.spec.ts @@ -1287,6 +1287,7 @@ describe('Localization', () => { locale: englishLocale, id: result.id, }) + const docEs = await payload.findByID({ collection: tabSlug, locale: spanishLocale, @@ -1406,7 +1407,7 @@ describe('Localization', () => { expect(collection.fields[3].fields[0].localized).toBeUndefined() }) }) - + describe('nested blocks', () => { let id it('should allow creating nested blocks per locale', async () => { @@ -1416,6 +1417,18 @@ describe('Localization', () => { content: [ { blockType: 'blockInsideBlock', + array: [ + { + link: { + label: 'English 1', + }, + }, + { + link: { + label: 'English 2', + }, + }, + ], content: [ { blockType: 'textBlock', @@ -1429,6 +1442,11 @@ describe('Localization', () => { id = doc.id + const retrievedInEN = await payload.findByID({ + collection: 'blocks-fields', + id, + }) + await payload.update({ collection: 'blocks-fields', id, @@ -1437,6 +1455,18 @@ describe('Localization', () => { content: [ { blockType: 'blockInsideBlock', + array: [ + { + link: { + label: 'Spanish 1', + }, + }, + { + link: { + label: 'Spanish 2', + }, + }, + ], content: [ { blockType: 'textBlock', @@ -1456,6 +1486,12 @@ describe('Localization', () => { expect(retrieved.content.en[0].content).toHaveLength(1) expect(retrieved.content.es[0].content).toHaveLength(1) + + expect(retrieved.content.en[0].array[0].link.label).toStrictEqual('English 1') + expect(retrieved.content.en[0].array[1].link.label).toStrictEqual('English 2') + + expect(retrieved.content.es[0].array[0].link.label).toStrictEqual('Spanish 1') + expect(retrieved.content.es[0].array[1].link.label).toStrictEqual('Spanish 2') }) }) @@ -1480,16 +1516,25 @@ describe('Localization', () => { blockName: '1', blockType: 'someBlock', relationWithinBlock: randomDoc.id, + myGroup: { + text: 'hello in english 1', + }, }, { blockName: '2', blockType: 'someBlock', relationWithinBlock: randomDoc.id, + myGroup: { + text: 'hello in english 2', + }, }, { blockName: '3', blockType: 'someBlock', relationWithinBlock: randomDoc.id, + myGroup: { + text: 'hello in english 3', + }, }, ] @@ -1498,16 +1543,25 @@ describe('Localization', () => { blockName: '1', blockType: 'someBlock', relationWithinBlock: randomDoc2.id, + myGroup: { + text: 'hello in spanish 1', + }, }, { blockName: '2', blockType: 'someBlock', relationWithinBlock: randomDoc2.id, + myGroup: { + text: 'hello in spanish 2', + }, }, { blockName: '3', blockType: 'someBlock', relationWithinBlock: randomDoc2.id, + myGroup: { + text: 'hello in spanish 3', + }, }, ]