Skip to content

Latest commit

 

History

History

better-use-as-title

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Better useAsTitle property for Payload 3.0

screen-capture.webm

Install

pnpm add @payload-enchants/better-use-as-title

Add into your payload.config.ts:

import { betterUseAsTitle } from '@payload-enchants/better-use-as-title';

export default buildConfig({
  // ...your config
  plugins: [
    betterUseAsTitle({
      // List of collections to apply a custom `useAsTitle`
      collections: [
        {
          // Collection slug
          slug: 'better-use-as-title-test',

          // `data` is the current document data, req is instance of PayloadRequest, from which you can get `payload` and `user`
          // could be asynchronous as well
          useAsTitle: ({ data, req }) =>
            `${data.firstName ?? ''} - ${data.secondName}, ${data.age} y.o`,

          // override properties for useAsTitle field for this collection
          fieldOverride: {
            name: 'customNameForCollection',
          },
        },
      ],
      // override properties for useAsTitle field globally
      fieldOverride: {
        name: 'customName',
      },
    }),
  ],
});

Note for data that already exist

You would need to write a custom script that updates all your docs like this

payload.update({
  collection: 'posts',
  where: {}
})