Skip to content

Commit 51f1c8e

Browse files
r1tsuuDanRibbens
andauthored
fix(db-postgres): localized groups/tabs and nested fields (#6158)
--------- Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
1 parent 09ad6e4 commit 51f1c8e

File tree

5 files changed

+441
-49
lines changed

5 files changed

+441
-49
lines changed

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

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
/* eslint-disable no-param-reassign */
2-
import type { Field, TabAsField } from 'payload'
3-
import type { SanitizedConfig } from 'payload'
1+
2+
import type { Field, SanitizedConfig , TabAsField } from 'payload'
43

54
import { fieldAffectsData } from 'payload/shared'
65

@@ -430,7 +429,12 @@ export const traverseFields = <T extends Record<string, unknown>>({
430429

431430
if (field.localized && Array.isArray(table._locales)) {
432431
table._locales.forEach((localeRow) => {
433-
valuesToTransform.push({ ref: localizedFieldData, table: localeRow })
432+
valuesToTransform.push({
433+
ref: localizedFieldData,
434+
table: {
435+
...localeRow,
436+
},
437+
})
434438
})
435439
} else {
436440
valuesToTransform.push({ ref: result, table })
@@ -445,52 +449,28 @@ export const traverseFields = <T extends Record<string, unknown>>({
445449
case 'tab':
446450
case 'group': {
447451
const groupFieldPrefix = `${fieldPrefix || ''}${field.name}_`
452+
const groupData = {}
453+
const locale = table._locale as string
454+
const refKey = field.localized && locale ? locale : field.name
455+
456+
if (field.localized && locale) delete table._locale
457+
ref[refKey] = traverseFields<Record<string, unknown>>({
458+
adapter,
459+
blocks,
460+
config,
461+
dataRef: groupData as Record<string, unknown>,
462+
deletions,
463+
fieldPrefix: groupFieldPrefix,
464+
fields: field.fields,
465+
numbers,
466+
path: `${sanitizedPath}${field.name}`,
467+
relationships,
468+
table,
469+
texts,
470+
})
448471

449-
if (field.localized) {
450-
if (typeof locale === 'string' && !ref[locale]) {
451-
ref[locale] = {}
452-
delete table._locale
453-
}
454-
455-
Object.entries(ref).forEach(([groupLocale, groupLocaleData]) => {
456-
ref[groupLocale] = traverseFields<Record<string, unknown>>({
457-
adapter,
458-
blocks,
459-
config,
460-
dataRef: groupLocaleData as Record<string, unknown>,
461-
deletions,
462-
fieldPrefix: groupFieldPrefix,
463-
fields: field.fields,
464-
numbers,
465-
path: `${sanitizedPath}${field.name}`,
466-
relationships,
467-
table,
468-
texts,
469-
})
470-
})
471-
if ('_order' in ref) {
472-
delete ref._order
473-
}
474-
} else {
475-
const groupData = {}
476-
477-
ref[field.name] = traverseFields<Record<string, unknown>>({
478-
adapter,
479-
blocks,
480-
config,
481-
dataRef: groupData as Record<string, unknown>,
482-
deletions,
483-
fieldPrefix: groupFieldPrefix,
484-
fields: field.fields,
485-
numbers,
486-
path: `${sanitizedPath}${field.name}`,
487-
relationships,
488-
table,
489-
texts,
490-
})
491-
if ('_order' in ref) {
492-
delete ref._order
493-
}
472+
if ('_order' in ref) {
473+
delete ref._order
494474
}
495475

496476
return
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { CollectionConfig } from 'payload/types'
2+
3+
export const groupSlug = 'groups'
4+
5+
export const Group: CollectionConfig = {
6+
slug: groupSlug,
7+
fields: [
8+
{
9+
name: 'groupLocalized',
10+
type: 'group',
11+
fields: [
12+
{
13+
name: 'title',
14+
type: 'text',
15+
},
16+
],
17+
localized: true,
18+
},
19+
{
20+
name: 'group',
21+
type: 'group',
22+
fields: [
23+
{
24+
name: 'title',
25+
type: 'text',
26+
localized: true,
27+
},
28+
],
29+
},
30+
{
31+
name: 'deep',
32+
type: 'group',
33+
fields: [
34+
{
35+
name: 'array',
36+
type: 'array',
37+
fields: [
38+
{
39+
name: 'title',
40+
type: 'text',
41+
localized: true,
42+
},
43+
],
44+
},
45+
{
46+
name: 'blocks',
47+
type: 'blocks',
48+
blocks: [
49+
{
50+
slug: 'first',
51+
fields: [
52+
{
53+
name: 'title',
54+
type: 'text',
55+
localized: true,
56+
},
57+
],
58+
},
59+
],
60+
},
61+
],
62+
},
63+
],
64+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { CollectionConfig } from 'payload/types'
2+
3+
export const tabSlug = 'tabs'
4+
5+
export const Tab: CollectionConfig = {
6+
slug: tabSlug,
7+
fields: [
8+
{
9+
type: 'tabs',
10+
tabs: [
11+
{
12+
name: 'tabLocalized',
13+
localized: true,
14+
fields: [
15+
{
16+
name: 'title',
17+
type: 'text',
18+
},
19+
],
20+
},
21+
{
22+
name: 'tab',
23+
fields: [
24+
{
25+
localized: true,
26+
name: 'title',
27+
type: 'text',
28+
},
29+
],
30+
},
31+
{
32+
name: 'deep',
33+
fields: [
34+
{
35+
name: 'array',
36+
type: 'array',
37+
fields: [
38+
{
39+
localized: true,
40+
type: 'text',
41+
name: 'title',
42+
},
43+
],
44+
},
45+
{
46+
name: 'blocks',
47+
type: 'blocks',
48+
blocks: [
49+
{
50+
slug: 'first',
51+
fields: [
52+
{
53+
localized: true,
54+
type: 'text',
55+
name: 'title',
56+
},
57+
],
58+
},
59+
],
60+
},
61+
],
62+
},
63+
],
64+
},
65+
],
66+
}

test/localization/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import type { LocalizedPost } from './payload-types.js'
77
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
88
import { devUser } from '../credentials.js'
99
import { ArrayCollection } from './collections/Array/index.js'
10+
import { Group } from './collections/Group/index.js'
1011
import { NestedToArrayAndBlock } from './collections/NestedToArrayAndBlock/index.js'
12+
import { Tab } from './collections/Tab/index.js'
1113
import {
1214
blocksWithLocalizedSameName,
1315
defaultLocale,
@@ -225,6 +227,8 @@ export default buildConfigWithDefaults({
225227
slug: 'dummy',
226228
},
227229
NestedToArrayAndBlock,
230+
Group,
231+
Tab,
228232
{
229233
slug: localizedSortSlug,
230234
access: openAccess,

0 commit comments

Comments
 (0)