Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/fields/text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ The slug field exposes a few top-level config options for easy customization:
| `fieldToUse` | The name of the field to use when generating the slug. This field must exist in the same collection. Defaults to `title`. |
| `position` | The position of the slug field. [More details](./overview#admin-options). |
| `required` | Require the slug field. Defaults to `true`. |
| `slugify` | Override the default slugify function. Receives the value of the `fieldToUse` field and must return a string. |

### Slug Overrides

Expand Down
7 changes: 5 additions & 2 deletions packages/payload/src/fields/baseFields/slug/generateSlug.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { FieldHook } from '../../config/types.js'
import type { SlugFieldArgs } from './index.js'

import { slugify } from '../../../utilities/slugify.js'
import { slugify as defaultSlugify } from '../../../utilities/slugify.js'
import { countVersions } from './countVersions.js'

/**
* This is a `BeforeChange` field hook used to auto-generate the `slug` field.
* See `slugField` for more details.
*/
export const generateSlug =
(fallback: string): FieldHook =>
(fallback: string, customSlugify?: SlugFieldArgs['slugify']): FieldHook =>
async (args) => {
const { collection, data, global, operation, originalDoc, value: isChecked } = args

const slugify = customSlugify || defaultSlugify

// Ensure user-defined slugs are not overwritten during create
// Use a generic falsy check here to include empty strings
if (operation === 'create') {
Expand Down
9 changes: 7 additions & 2 deletions packages/payload/src/fields/baseFields/slug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FieldAdmin, RowField } from '../../../fields/config/types.js'

import { generateSlug } from './generateSlug.js'

type SlugFieldArgs = {
export type SlugFieldArgs = {
/**
* Override for the `generateSlug` checkbox field name.
* @default 'generateSlug'
Expand Down Expand Up @@ -39,6 +39,10 @@ type SlugFieldArgs = {
* @default true
*/
required?: boolean
/**
* Provide your own slugify function to override the default.
*/
slugify?: (val?: string) => string
}

type SlugField = (args?: SlugFieldArgs) => RowField
Expand Down Expand Up @@ -69,6 +73,7 @@ export const slugField: SlugField = ({
overrides,
position = 'sidebar',
required = true,
slugify,
} = {}) => {
const baseField: RowField = {
type: 'row',
Expand All @@ -90,7 +95,7 @@ export const slugField: SlugField = ({
},
defaultValue: true,
hooks: {
beforeChange: [generateSlug(fieldToUse)],
beforeChange: [generateSlug(fieldToUse, slugify)],
},
},
{
Expand Down
Loading