Skip to content

How to support auto generate slug from title field in collections #584

Answered by Stupidism
Stupidism asked this question in Q&A
Discussion options

You must be logged in to vote

A better solution for slug is to add a custom element:

// Articles:

fields: [
  // ...
  slug({ trackingField: 'title' }),
  // ...
]

// fields/slug.ts

import { Field } from 'payload/types';
import { merge } from 'lodash';

import { getSlugInput } from './ui/SlugInput';

type Slug = (
  options?: { trackingField?: string },
  overrides?: Partial<Field>
) => Field;

// By dynamically building fields in code configurations are reusable and concise
export const slug: Slug = ({ trackingField = 'title' } = {}, overrides) =>
  merge<Field, Partial<Field> | undefined>(
    {
      name: 'slug',
      unique: true,
      type: 'text',
      admin: {
        position: 'sidebar',
        compone…

Replies: 5 comments 6 replies

Comment options

You must be logged in to vote
1 reply
@Stupidism
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@Stupidism
Comment options

@MinSomai
Comment options

@jmikrut
Comment options

@MinSomai
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Stupidism
Comment options

You must be logged in to vote
1 reply
@designly1
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants