diff --git a/packages/payload/src/utilities/traverseFields.ts b/packages/payload/src/utilities/traverseFields.ts index 189aaa504f2..0c0745756fe 100644 --- a/packages/payload/src/utilities/traverseFields.ts +++ b/packages/payload/src/utilities/traverseFields.ts @@ -1,7 +1,33 @@ -import type { Field, TabAsField } from '../fields/config/types.js' +import type { ArrayField, BlocksField, Field, TabAsField } from '../fields/config/types.js' import { fieldHasSubFields } from '../fields/config/types.js' +const traverseArrayOrBlocksField = ({ + callback, + data, + field, + parentRef, +}: { + callback: TraverseFieldsCallback + data: Record[] + field: ArrayField | BlocksField + parentRef?: unknown +}) => { + for (const ref of data) { + let fields: Field[] + if (field.type === 'blocks' && typeof ref?.blockType === 'string') { + const block = field.blocks.find((block) => block.slug === ref.blockType) + fields = block?.fields + } else if (field.type === 'array') { + fields = field.fields + } + + if (fields) { + traverseFields({ callback, fields, parentRef, ref }) + } + } +} + export type TraverseFieldsCallback = (args: { /** * The current field @@ -68,11 +94,11 @@ export const traverseFields = ({ }) return } - if (field.type !== 'tab' && fieldHasSubFields(field)) { + if (field.type !== 'tab' && (fieldHasSubFields(field) || field.type === 'blocks')) { const parentRef = ref if ('name' in field && field.name) { if (typeof ref[field.name] === 'undefined') { - if (field.type === 'array') { + if (field.type === 'array' || field.type === 'blocks') { if (field.localized) { ref[field.name] = {} } else { @@ -85,7 +111,33 @@ export const traverseFields = ({ } ref = ref[field.name] } - traverseFields({ callback, fields: field.fields, parentRef, ref }) + + if (field.type === 'blocks' || field.type === 'array') { + if (field.localized) { + for (const key in (ref ?? {}) as Record) { + const localeData = ref[key] + if (!Array.isArray(localeData)) { + continue + } + + traverseArrayOrBlocksField({ + callback, + data: localeData, + field, + parentRef, + }) + } + } else if (Array.isArray(ref)) { + traverseArrayOrBlocksField({ + callback, + data: ref, + field, + parentRef, + }) + } + } else { + traverseFields({ callback, fields: field.fields, parentRef, ref }) + } } }) } diff --git a/test/joins/int.spec.ts b/test/joins/int.spec.ts index 1f39896a143..a89dac2c493 100644 --- a/test/joins/int.spec.ts +++ b/test/joins/int.spec.ts @@ -643,7 +643,6 @@ describe('Joins Field', () => { } }` - expect(true).toBeTruthy() const response = await restClient .GRAPHQL_POST({ body: JSON.stringify({ query }) }) .then((res) => res.json())