diff --git a/packages/plugin-nested-docs/src/fields/parent.ts b/packages/plugin-nested-docs/src/fields/parent.ts index 92212cb451d..32048ab32ec 100644 --- a/packages/plugin-nested-docs/src/fields/parent.ts +++ b/packages/plugin-nested-docs/src/fields/parent.ts @@ -9,23 +9,15 @@ export const createParentField = ( >, ): SingleRelationshipField => ({ name: 'parent', - relationTo, - type: 'relationship', - maxDepth: 1, - filterOptions: ({ id }) => { - if (id) { - return { - id: { not_equals: id }, - 'breadcrumbs.doc': { not_in: [id] }, - } - } - - return null - }, admin: { position: 'sidebar', ...(overrides?.admin || {}), }, + // filterOptions are assigned dynamically based on the pluginConfig + // filterOptions: parentFilterOptions(), + maxDepth: 1, + relationTo, + type: 'relationship', ...(overrides || {}), }) diff --git a/packages/plugin-nested-docs/src/fields/parentFilterOptions.ts b/packages/plugin-nested-docs/src/fields/parentFilterOptions.ts new file mode 100644 index 00000000000..fc408820f70 --- /dev/null +++ b/packages/plugin-nested-docs/src/fields/parentFilterOptions.ts @@ -0,0 +1,16 @@ +import type { FilterOptions } from 'payload/dist/fields/config/types' + +export const parentFilterOptions: (breadcrumbsFieldSlug?: string) => FilterOptions = + (breadcrumbsFieldSlug = 'breadcrumbs') => + ({ id }) => { + if (id) { + return { + id: { not_equals: id }, + [`${breadcrumbsFieldSlug}.doc`]: { not_in: [id] }, + } + } + + return null + } + +export default parentFilterOptions diff --git a/packages/plugin-nested-docs/src/index.ts b/packages/plugin-nested-docs/src/index.ts index b792c7a42c7..2ae37e5e98c 100644 --- a/packages/plugin-nested-docs/src/index.ts +++ b/packages/plugin-nested-docs/src/index.ts @@ -1,9 +1,11 @@ import type { Plugin } from 'payload/config' +import type { SingleRelationshipField } from 'payload/dist/fields/config/types' import type { PluginConfig } from './types' import createBreadcrumbsField from './fields/breadcrumbs' import createParentField from './fields/parent' +import parentFilterOptions from './fields/parentFilterOptions' import resaveChildren from './hooks/resaveChildren' import resaveSelfAfterCreate from './hooks/resaveSelfAfterCreate' import populateBreadcrumbs from './utilities/populateBreadcrumbs' @@ -23,10 +25,20 @@ const nestedDocs = const existingParentField = collection.fields.find( (field) => 'name' in field && field.name === (pluginConfig?.parentFieldSlug || 'parent'), - ) + ) as SingleRelationshipField + + const defaultFilterOptions = parentFilterOptions(pluginConfig?.breadcrumbsFieldSlug) + + if (existingParentField) { + if (!existingParentField.filterOptions) { + existingParentField.filterOptions = defaultFilterOptions + } + } if (!existingParentField && !pluginConfig.parentFieldSlug) { - fields.push(createParentField(collection.slug)) + const defaultParentField = createParentField(collection.slug) + defaultParentField.filterOptions = defaultFilterOptions + fields.push(defaultParentField) } if (!existingBreadcrumbField && !pluginConfig.breadcrumbsFieldSlug) { diff --git a/test/plugin-nested-docs/int.spec.ts b/test/plugin-nested-docs/int.spec.ts index d574784e4bb..ace3e0b1af4 100644 --- a/test/plugin-nested-docs/int.spec.ts +++ b/test/plugin-nested-docs/int.spec.ts @@ -68,5 +68,34 @@ describe('Nested Docs', () => { expect(parentField.admin.description).toStrictEqual('custom') }) + + it('should allow custom breadcrumb slugs', async () => { + const parent = await payload.create({ + collection: 'categories', + data: { + name: 'parent', + }, + }) + const child = await payload.create({ + collection: 'categories', + data: { + name: 'child', + parent: parent.id, + }, + }) + const grandchild = await payload.create({ + collection: 'categories', + data: { + name: 'grandchild', + parent: child.id, + }, + }) + + expect(grandchild.categorization[0].doc).toStrictEqual(parent.id) + expect(grandchild.categorization[0].label).toStrictEqual('parent') + expect(grandchild.categorization[1].doc).toStrictEqual(child.id) + expect(grandchild.categorization[1].label).toStrictEqual('child') + expect(grandchild.categorization[2].label).toStrictEqual('grandchild') + }) }) })