You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Continuation of #14007.
Supports overriding the default slugify function of the slug field. This
is necessary if the slug requires special treatment, such as character
encoding, additional language support, etc.
For example, if you wanted to use the
[`slugify`](https://www.npmjs.com/package/slugify) package, you could do
something like this:
```ts
import type { CollectionConfig } from 'payload'
import { slugField } from 'payload'
import slugify from 'slugify';
export const MyCollection: CollectionConfig = {
// ...
fields: [
// ...
slugField({
slugify: ({ valueToSlugify }) => slugify(valueToSlugify, {
// ...additional `slugify` options here
})
})
]
}
```
This PR also deprecates the old `fieldToUse` arg in favor of `useAsSlug`
which is more semantically clear, following the same convention as
`useAsTitle`.
Example:
```ts
import type { CollectionConfig } from 'payload'
import { slugField } from 'payload'
export const MyCollection: CollectionConfig = {
// ...
fields: [
// ...
slugField({
useAsSlug: 'myCustomTitle'
})
]
}
```
In follow-up PRs, we should also:
- Improve the default slugify function to better handle special
characters on its own, etc.
- [Support nested
slugs](#14783)
Copy file name to clipboardExpand all lines: docs/fields/text.mdx
+37-1Lines changed: 37 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -217,10 +217,11 @@ The slug field exposes a few top-level config options for easy customization:
217
217
|`name`| To be used as the slug field's name. Defaults to `slug`. |
218
218
|`overrides`| A function that receives the default fields so you can override on a granular level. See example below. [More details](#slug-overrides). |
219
219
|`checkboxName`| To be used as the name for the `generateSlug` checkbox field. Defaults to `generateSlug`. |
220
-
|`fieldToUse`| The name of the field to use when generating the slug. This field must exist in the same collection. Defaults to `title`.|
220
+
|`useAsSlug`| The name of the top-level field to use when generating the slug. This field must exist in the same collection. Defaults to `title`. |
221
221
|`localized`| Enable localization on the `slug` and `generateSlug` fields. Defaults to `false`. |
222
222
|`position`| The position of the slug field. [More details](./overview#admin-options). |
223
223
|`required`| Require the slug field. Defaults to `true`. |
224
+
|`slugify`| Override the default slugify function. [More details](#custom-slugify-function). |
You can also override the default slugify function of the slug field. This is necessary if the slug requires special treatment, such as character encoding, additional language support, etc.
253
+
254
+
This functions receives the value of the `useAsSlug` field as `valueToSlugify` and must return a string.
255
+
256
+
For example, if you wanted to use the [`slugify`](https://www.npmjs.com/package/slugify) package, you could do something like this:
257
+
258
+
```ts
259
+
importtype { CollectionConfig } from'payload'
260
+
import { slugField } from'payload'
261
+
importslugifyfrom'slugify'
262
+
263
+
exportconst MyCollection:CollectionConfig= {
264
+
// ...
265
+
fields: [
266
+
// ...
267
+
slugField({
268
+
slugify: ({ valueToSlugify }) =>
269
+
slugify(valueToSlugify, {
270
+
// ...additional `slugify` options here
271
+
}),
272
+
}),
273
+
],
274
+
}
275
+
```
276
+
277
+
The following args are provided to the custom `slugify` function:
0 commit comments