Skip to content

Commit e4a9029

Browse files
authored
feat(plugin-redirects)!: update fields overrides to use a function (#6675)
## Description Updates the `fields` override in plugin redirects to allow for overriding ```ts // before overrides: { fields: [ { type: 'text', name: 'customField', }, ], }, // current overrides: { fields: ({ defaultFields }) => { return [ ...defaultFields, { type: 'text', name: 'customField', }, ] }, }, ``` ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
1 parent 7c8d562 commit e4a9029

File tree

4 files changed

+91
-107
lines changed

4 files changed

+91
-107
lines changed

packages/plugin-redirects/src/deepMerge.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
11
import type { Config } from 'payload/config'
2+
import type { CollectionConfig, Field } from 'payload/types'
23

34
import type { RedirectsPluginConfig } from './types.js'
45

5-
import deepMerge from './deepMerge.js'
6-
76
export const redirectsPlugin =
87
(pluginConfig: RedirectsPluginConfig) =>
9-
(incomingConfig: Config): Config => ({
10-
...incomingConfig,
11-
collections: [
12-
...(incomingConfig?.collections || []),
13-
deepMerge(
14-
{
15-
slug: 'redirects',
16-
access: {
17-
read: (): boolean => true,
18-
},
19-
admin: {
20-
defaultColumns: ['from', 'to.type', 'createdAt'],
8+
(incomingConfig: Config): Config => {
9+
const defaultFields: Field[] = [
10+
{
11+
name: 'from',
12+
type: 'text',
13+
index: true,
14+
label: 'From URL',
15+
required: true,
16+
},
17+
{
18+
name: 'to',
19+
type: 'group',
20+
fields: [
21+
{
22+
name: 'type',
23+
type: 'radio',
24+
admin: {
25+
layout: 'horizontal',
26+
},
27+
defaultValue: 'reference',
28+
label: 'To URL Type',
29+
options: [
30+
{
31+
label: 'Internal link',
32+
value: 'reference',
33+
},
34+
{
35+
label: 'Custom URL',
36+
value: 'custom',
37+
},
38+
],
2139
},
22-
fields: [
23-
{
24-
name: 'from',
25-
type: 'text',
26-
index: true,
27-
label: 'From URL',
28-
required: true,
40+
{
41+
name: 'reference',
42+
type: 'relationship',
43+
admin: {
44+
condition: (_, siblingData) => siblingData?.type === 'reference',
2945
},
30-
{
31-
name: 'to',
32-
type: 'group',
33-
fields: [
34-
{
35-
name: 'type',
36-
type: 'radio',
37-
admin: {
38-
layout: 'horizontal',
39-
},
40-
defaultValue: 'reference',
41-
label: 'To URL Type',
42-
options: [
43-
{
44-
label: 'Internal link',
45-
value: 'reference',
46-
},
47-
{
48-
label: 'Custom URL',
49-
value: 'custom',
50-
},
51-
],
52-
},
53-
{
54-
name: 'reference',
55-
type: 'relationship',
56-
admin: {
57-
condition: (_, siblingData) => siblingData?.type === 'reference',
58-
},
59-
label: 'Document to redirect to',
60-
relationTo: pluginConfig?.collections || [],
61-
required: true,
62-
},
63-
{
64-
name: 'url',
65-
type: 'text',
66-
admin: {
67-
condition: (_, siblingData) => siblingData?.type === 'custom',
68-
},
69-
label: 'Custom URL',
70-
required: true,
71-
},
72-
],
73-
label: false,
46+
label: 'Document to redirect to',
47+
relationTo: pluginConfig?.collections || [],
48+
required: true,
49+
},
50+
{
51+
name: 'url',
52+
type: 'text',
53+
admin: {
54+
condition: (_, siblingData) => siblingData?.type === 'custom',
7455
},
75-
],
76-
},
77-
pluginConfig?.overrides || {},
78-
),
79-
],
80-
})
56+
label: 'Custom URL',
57+
required: true,
58+
},
59+
],
60+
label: false,
61+
},
62+
]
63+
64+
const redirectsCollection: CollectionConfig = {
65+
...(pluginConfig?.overrides || {}),
66+
slug: pluginConfig?.overrides?.slug || 'redirects',
67+
access: {
68+
read: () => true,
69+
...(pluginConfig?.overrides?.access || {}),
70+
},
71+
admin: {
72+
defaultColumns: ['from', 'to.type', 'createdAt'],
73+
...(pluginConfig?.overrides?.admin || {}),
74+
},
75+
fields:
76+
pluginConfig?.overrides?.fields && typeof pluginConfig?.overrides?.fields === 'function'
77+
? pluginConfig?.overrides.fields({ defaultFields })
78+
: defaultFields,
79+
}
80+
81+
return {
82+
...incomingConfig,
83+
collections: [...(incomingConfig?.collections || []), redirectsCollection],
84+
}
85+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type { CollectionConfig } from 'payload/types'
1+
import type { CollectionConfig, Field } from 'payload/types'
2+
3+
export type FieldsOverride = (args: { defaultFields: Field[] }) => Field[]
24

35
export type RedirectsPluginConfig = {
46
collections?: string[]
5-
overrides?: Partial<CollectionConfig>
7+
overrides?: Partial<Omit<CollectionConfig, 'fields'>> & { fields: FieldsOverride }
68
}

test/plugin-redirects/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ export default buildConfigWithDefaults({
2727
plugins: [
2828
redirectsPlugin({
2929
collections: ['pages'],
30+
overrides: {
31+
fields: ({ defaultFields }) => {
32+
return [
33+
...defaultFields,
34+
{
35+
type: 'text',
36+
name: 'customField',
37+
},
38+
]
39+
},
40+
},
3041
}),
3142
],
3243
})

0 commit comments

Comments
 (0)