Like what we’re doing? Star us on GitHub!

use a group field for useAsTitle

Teelon
5 months ago
1 1

Can I use a group field for the useAsTitle on a collection admin property?. As of now it takes String? which indicates the field name that you want to use.
{ name:'name', type:'group', fields:[ { name:'first', type :'text' }, { name:'last', type:'text' } ] }

  • JarrodMFlesch
    Payload Team
    5 months ago

    To be clear, you are wanting to use first as the document title?

    No that is not currently possible. What you could do instead is create a top level field i.e. fullName and use a field hook to update that based on name.first and name.last.

    Something like this:

    {
      slug: 'example-collection',
      admin: {
        useAsTitle: 'fullName',
      },
      fields: [
        {
          admin: {
            hidden: true,
          },
          name: 'fullName',
          type: 'text',
          hooks: {
            beforeChange: [
              ({ siblingData, operation, value }) => {
                if (operation === 'create' || operation === 'update') {
                  return `${siblingData.name.first} ${siblingData.name.last}`
                }
                return value
              },
            ],
          },
        },
        {
          name: 'name',
          type: 'group',
          fields: [
            {
              name: 'first',
              type: 'text',
            },
            {
              name: 'last',
              type: 'text',
            },
          ],
        },
      ],
    },
Open the post
Continue the discussion in GitHub
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More