Files
payload/test/fields/collections/Blocks/index.ts
Alessio Gravili e6fea1d132 fix: localized fields within block references were not handled properly if any parent is localized (#11207)
The `localized` properly was not stripped out of referenced block fields, if any parent was localized. For normal fields, this is done in sanitizeConfig. As the same referenced block config can be used in both a localized and non-localized config, we are not able to strip it out inside sanitizeConfig by modifying the block config.

Instead, this PR had to bring back tedious logic to handle it everywhere the `field.localized` property is accessed. For backwards-compatibility, we need to keep the existing sanitizeConfig logic. In 4.0, we should remove it to benefit from better test coverage of runtime field.localized handling - for now, this is done for our test suite using the `PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY` flag.
2025-02-17 19:50:32 +00:00

431 lines
9.4 KiB
TypeScript

import type { BlocksField, CollectionConfig } from 'payload'
import { slateEditor } from '@payloadcms/richtext-slate'
import { blockFieldsSlug, textFieldsSlug } from '../../slugs.js'
import { getBlocksFieldSeedData } from './shared.js'
export const getBlocksField = (prefix?: string): BlocksField => ({
name: 'blocks',
type: 'blocks',
blocks: [
{
slug: prefix ? `${prefix}Content` : 'content',
interfaceName: prefix ? `${prefix}ContentBlock` : 'ContentBlock',
admin: {
components: {
Label: './collections/Blocks/components/CustomBlockLabel.tsx',
},
},
fields: [
{
name: 'text',
type: 'text',
required: true,
},
{
name: 'richText',
type: 'richText',
editor: slateEditor({}),
},
],
},
{
slug: prefix ? `${prefix}Number` : 'number',
interfaceName: prefix ? `${prefix}NumberBlock` : 'NumberBlock',
fields: [
{
name: 'number',
type: 'number',
required: true,
},
],
},
{
slug: prefix ? `${prefix}SubBlocks` : 'subBlocks',
interfaceName: prefix ? `${prefix}SubBlocksBlock` : 'SubBlocksBlock',
fields: [
{
type: 'collapsible',
fields: [
{
name: 'subBlocks',
type: 'blocks',
blocks: [
{
slug: 'textRequired',
fields: [
{
name: 'text',
type: 'text',
required: true,
},
],
},
{
slug: 'number',
interfaceName: 'NumberBlock',
fields: [
{
name: 'number',
type: 'number',
required: true,
},
],
},
],
},
],
label: 'Collapsible within Block',
},
],
},
{
slug: prefix ? `${prefix}Tabs` : 'tabs',
interfaceName: prefix ? `${prefix}TabsBlock` : 'TabsBlock',
fields: [
{
type: 'tabs',
tabs: [
{
fields: [
{
type: 'collapsible',
fields: [
{
// collapsible
name: 'textInCollapsible',
type: 'text',
},
],
label: 'Collapsible within Block',
},
{
type: 'row',
fields: [
{
// collapsible
name: 'textInRow',
type: 'text',
},
],
},
],
label: 'Tab with Collapsible',
},
],
},
],
},
],
defaultValue: getBlocksFieldSeedData(prefix),
required: true,
})
const BlockFields: CollectionConfig = {
slug: blockFieldsSlug,
fields: [
getBlocksField(),
{
...getBlocksField(),
name: 'duplicate',
},
{
...getBlocksField('localized'),
name: 'collapsedByDefaultBlocks',
admin: {
initCollapsed: true,
},
localized: true,
},
{
...getBlocksField('localized'),
name: 'disableSort',
admin: {
isSortable: false,
},
localized: true,
},
{
...getBlocksField('localized'),
name: 'localizedBlocks',
localized: true,
},
{
name: 'i18nBlocks',
type: 'blocks',
blocks: [
{
slug: 'textInI18nBlock',
fields: [
{
name: 'text',
type: 'text',
},
],
graphQL: {
singularName: 'I18nText',
},
labels: {
plural: {
en: 'Texts en',
es: 'Texts es',
},
singular: {
en: 'Text en',
es: 'Text es',
},
},
},
],
label: {
en: 'Block en',
es: 'Block es',
},
labels: {
plural: {
en: 'Blocks en',
es: 'Blocks es',
},
singular: {
en: 'Block en',
es: 'Block es',
},
},
},
{
name: 'blocksWithLocalizedArray',
type: 'blocks',
blocks: [
{
slug: 'localizedArray',
fields: [
{
name: 'array',
type: 'array',
localized: true,
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
],
},
{
name: 'blocksWithSimilarConfigs',
type: 'blocks',
blocks: [
{
slug: 'block-a',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title',
type: 'text',
required: true,
},
],
},
],
},
{
slug: 'block-b',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title2',
type: 'text',
required: true,
},
],
},
],
},
{
slug: 'group-block',
fields: [
{
name: 'group',
type: 'group',
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
],
},
{
name: 'blocksWithSimilarGroup',
type: 'blocks',
admin: {
description:
'The purpose of this field is to test validateExistingBlockIsIdentical works with similar blocks with group fields',
},
blocks: [
{
slug: 'group-block',
fields: [
{
name: 'group',
type: 'group',
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
{
slug: 'block-b',
fields: [
{
name: 'items',
type: 'array',
fields: [
{
name: 'title2',
type: 'text',
required: true,
},
],
},
],
},
],
},
{
name: 'blocksWithMinRows',
type: 'blocks',
blocks: [
{
slug: 'blockWithMinRows',
fields: [
{
name: 'blockTitle',
type: 'text',
},
],
},
],
minRows: 2,
},
{
name: 'customBlocks',
type: 'blocks',
blocks: [
{
slug: 'block-1',
fields: [
{
name: 'block1Title',
type: 'text',
},
],
},
{
slug: 'block-2',
fields: [
{
name: 'block2Title',
type: 'text',
},
],
},
],
},
{
name: 'ui',
type: 'ui',
admin: {
components: {
Field: '/collections/Blocks/components/AddCustomBlocks/index.js#AddCustomBlocks',
},
},
},
{
name: 'relationshipBlocks',
type: 'blocks',
blocks: [
{
slug: 'relationships',
fields: [
{
name: 'relationship',
type: 'relationship',
relationTo: textFieldsSlug,
},
],
},
],
},
{
name: 'blockWithLabels',
type: 'blocks',
labels: {
singular: ({ t }) => t('authentication:account'),
plural: ({ t }) => t('authentication:generate'),
},
blocks: [
{
labels: {
singular: ({ t }) => t('authentication:account'),
plural: ({ t }) => t('authentication:generate'),
},
slug: 'text',
fields: [
{
name: 'text',
type: 'text',
},
],
},
],
},
{
name: 'deduplicatedBlocks',
type: 'blocks',
blockReferences: ['ConfigBlockTest'],
blocks: [],
},
{
name: 'deduplicatedBlocks2',
type: 'blocks',
blockReferences: ['ConfigBlockTest'],
blocks: [],
},
{
name: 'localizedReferencesLocalizedBlock',
type: 'blocks',
blockReferences: ['localizedTextReference'],
blocks: [],
localized: true,
},
{
name: 'localizedReferences',
type: 'blocks',
// Needs to be a separate block - otherwise this will break in postgres. This is unrelated to block references
// and an issue with all blocks.
blockReferences: ['localizedTextReference2'],
blocks: [],
},
],
}
export default BlockFields