Skip to content

Commit 57bde77

Browse files
authored
fix(drizzle): groups inside localized tabs are not working correctly (#15936)
Fixes #11651 The issue was related to 2 things: 1. `forceLocalized` was not passed through if the current field is not localized but had `forceLocalized: true` because the parent field is localized, e.g: localized tab -> non localized group -> non localized text - in the DB schema we need to have the text field in the `_locales` table. 2. `forceLocale` was not passed through in `transform/write` for tab/group fields
1 parent 2646018 commit 57bde77

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

packages/drizzle/src/schema/traverseFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ export const traverseFields = ({
668668
disableUnique,
669669
fieldPrefix: `${fieldName}.`,
670670
fields: field.flattenedFields,
671-
forceLocalized: isFieldLocalized,
671+
forceLocalized: isFieldLocalized || Boolean(forceLocalized),
672672
indexes,
673673
localesColumns,
674674
localesIndexes,

packages/drizzle/src/transform/write/traverseFields.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,11 @@ export const traverseFields = ({
356356
blocksToDelete,
357357
columnPrefix: `${columnName}_`,
358358
data: groupData,
359+
enableAtomicWrites,
359360
existingLocales,
360361
fieldPrefix: `${fieldName}_`,
361362
fields: field.flattenedFields,
363+
forcedLocale,
362364
insideArrayOrBlock,
363365
locales,
364366
numbers,

test/localization/collections/Tab/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ export const Tab: CollectionConfig = {
2626
},
2727
],
2828
},
29+
{
30+
name: 'group',
31+
type: 'group',
32+
fields: [
33+
{
34+
name: 'heading',
35+
type: 'text',
36+
},
37+
],
38+
},
2939
],
3040
},
3141
{

test/localization/int.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import type {
1414
WithLocalizedRelationship,
1515
} from './payload-types.js'
1616

17-
import { devUser } from '../credentials.js'
1817
import { isMongoose, mongooseList } from '../__helpers/shared/isMongoose.js'
18+
import { devUser } from '../credentials.js'
1919

2020
// eslint-disable-next-line payload/no-relative-monorepo-imports
2121
import { copyDataFromLocaleHandler } from '../../packages/ui/src/utilities/copyDataFromLocale.js'
@@ -1273,13 +1273,15 @@ describe('Localization', () => {
12731273
})
12741274

12751275
if (isMongoose(payload)) {
1276+
// eslint-disable-next-line vitest/no-conditional-expect
12761277
expect(docWithoutFallback.items).toStrictEqual(null)
12771278
} else {
12781279
// TODO: build out compatability with SQL databases
12791280
// Currently SQL databases always fallback since the localized values are joined in.
12801281
// The join only has 2 states, undefined or the localized value of the requested locale.
12811282
// If the localized value is not in the DB, there is no way to know if the value should fallback or not so we fallback if fallbackLocale is truthy.
12821283
// In MongoDB the value can be set to null, which allows us to know that the value should fallback.
1284+
// eslint-disable-next-line vitest/no-conditional-expect
12831285
expect(docWithoutFallback.items).toStrictEqual(englishDoc.items)
12841286
}
12851287
})
@@ -2020,6 +2022,48 @@ describe('Localization', () => {
20202022
expect(docEs.deep.array[0].title).toBe('hello es')
20212023
expect(docEs.deep.blocks[0].title).toBe('hello es')
20222024
})
2025+
2026+
it('should properly isolate locales for a group inside a localized tab', async () => {
2027+
const docEs = await payload.create({
2028+
collection: tabSlug,
2029+
locale: spanishLocale,
2030+
data: {
2031+
tabLocalized: {
2032+
group: {
2033+
heading: 'Spanish heading',
2034+
},
2035+
},
2036+
},
2037+
})
2038+
2039+
await payload.update({
2040+
collection: tabSlug,
2041+
locale: englishLocale,
2042+
id: docEs.id,
2043+
data: {
2044+
tabLocalized: {
2045+
group: {
2046+
heading: 'English heading',
2047+
},
2048+
},
2049+
},
2050+
})
2051+
2052+
const readEn = await payload.findByID({
2053+
collection: tabSlug,
2054+
locale: englishLocale,
2055+
id: docEs.id,
2056+
})
2057+
2058+
const readEs = await payload.findByID({
2059+
collection: tabSlug,
2060+
locale: spanishLocale,
2061+
id: docEs.id,
2062+
})
2063+
2064+
expect(readEn.tabLocalized.group.heading).toBe('English heading')
2065+
expect(readEs.tabLocalized.group.heading).toBe('Spanish heading')
2066+
})
20232067
})
20242068

20252069
// Nested localized fields do no longer have their localized property stripped in

test/localization/payload-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,9 @@ export interface Tab {
762762
id?: string | null;
763763
}[]
764764
| null;
765+
group?: {
766+
heading?: string | null;
767+
};
765768
};
766769
tab?: {
767770
title?: string | null;
@@ -1586,6 +1589,11 @@ export interface TabsSelect<T extends boolean = true> {
15861589
title?: T;
15871590
id?: T;
15881591
};
1592+
group?:
1593+
| T
1594+
| {
1595+
heading?: T;
1596+
};
15891597
};
15901598
tab?:
15911599
| T

0 commit comments

Comments
 (0)