Files
payloadcms/test/admin/collections/FormatDocURL/index.ts
Patrik 1d1240fd13 feat: adds admin.formatDocURL function to control list view linking (#13773)
### What?

Adds a new `formatDocURL` function to collection admin configuration
that allows users to control the linkable state and URLs of first column
fields in list views.

### Why?

To provide a way to disable automatic link creation from the first
column or provide custom URLs based on document data, user permissions,
view context, and document state.

### How?

- Added `formatDocURL` function type to `CollectionAdminOptions` that
receives document data, default URL, request context, collection slug,
and view type
- Modified `renderCell` to call the function when available and handle
three return types:
  - `null`: disables linking entirely
  - `string`: uses custom URL
  - other: falls back to no linking for safety
- Added function to server-only properties to prevent React Server
Components serialization issues
- Updated `DefaultCell` component to support custom `linkURL` prop


---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1211211792037945
2025-09-24 12:20:54 -07:00

57 lines
1.5 KiB
TypeScript

import type { CollectionConfig } from 'payload'
export const FormatDocURL: CollectionConfig = {
slug: 'format-doc-url',
admin: {
// Custom formatDocURL function to control linking behavior
formatDocURL: ({ doc, defaultURL, req, collectionSlug, viewType }) => {
// Disable linking for documents with title 'no-link'
if (doc.title === 'no-link') {
return null
}
// Custom link for documents with title 'custom-link'
if (doc.title === 'custom-link') {
return '/custom-destination'
}
// Example: Add query params based on user email (fallback for normal cases)
if (
req.user?.email === 'dev@payloadcms.com' &&
viewType !== 'trash' &&
doc._status === 'draft'
) {
return defaultURL + '?admin=true'
}
// Example: Different behavior in trash view (check this before user-specific logic)
if (viewType === 'trash') {
return defaultURL + '?from=trash'
}
// Example: Collection-specific behavior for published docs
if (collectionSlug === 'format-doc-url' && doc._status === 'published') {
return defaultURL + '?published=true'
}
// For all other documents, just return the default URL
return defaultURL
},
},
trash: true,
versions: {
drafts: true,
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'description',
type: 'textarea',
},
],
}