Skip to content

Conversation

@sondreorland
Copy link
Contributor

What?

Adds support for custom slugify functions in the Payload Config. With the option to override it at field level.

Why?

#14117 added support for custom slugify functions on field level, which is very useful but it makes the user have to add their custom slugify function to every single place they need a slug field. By setting it in the payload config instead, it's just done once for all slug fields across the cms.

How?

Added slugify property to the main Config type
Updated generateSlug.ts to check for config-level slugify with priority order.
Updated slugifyHandler server function to access config-level slugify via req.payload.config.slugify
Added slugify to server-only config properties to prevent client-side errors

Can be used like this:

// payload.config.ts
slugify: ({ valueToSlugify }) => slugify(valueToSlugify, {
  // ...additional `slugify` options here
})

Priority order:

  1. Field-level slugify (if defined)
  2. Config-level slugify (if defined)
  3. Default slugify function

Example with config level and field level slugify

// payload.config.ts
slugify: ({ valueToSlugify }) => {
  return `config-${valueToSlugify?.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '')}`
},
screenshot-slugify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant