Make useAsTitle formatable #3257
Replies: 6 comments 13 replies
-
What we always do is add an hidden field and handle the logic in the beforeChange hook. This way you can also use it in your frondend. {
name: "title",
type: "text",
required: true,
admin: {
hidden: true,
},
hooks: {
beforeChange: [
async ({ data }) => {
// fallback?
return `$(data.firstname) - $(data.lastname)`
}
]
}
}, |
Beta Was this translation helpful? Give feedback.
-
@GeorgeHulpoi Do you have a code snippet of how your proposed solution will look? To me, it looks like almost the same pattern, only now there is an extra field. Or is the case that you're making, to make it more intuitive for developers to add logic to the title the cleanest way possible? Maybe adding an beforeShowingTitle hook here can tackle this: (would be admin focused, so it's not triggered in the API) Also, would be an idea to add some documentation for making dynamic titles in the admin UI. |
Beta Was this translation helpful? Give feedback.
-
@matthijs166 A very simple example would be to add a condition to the export const formatUseAsTitle = (args: {
field?: FormField
doc?: Record<string, any>
collection: SanitizedCollectionConfig
i18n: typeof i18next
config: SanitizedConfig
}): string => {
const {
field: fieldFromProps,
doc,
collection,
collection: {
admin: { useAsTitle },
},
i18n,
config: {
admin: {
dateFormat: dateFormatFromConfig,
},
},
} = args;
if (!fieldFromProps && !doc) {
return '';
}
if (typeof collection.admin.useAsTitle === 'function') {
collection.admin.useAsTitle.call({ doc })
}
const field = fieldFromProps || getObjectDotNotation<FormField>(doc, collection.admin.useAsTitle);
let title = typeof field === 'string' ? field : field?.value as string;
const fieldConfig = collection?.fields?.find((f) => 'name' in f && f?.name === useAsTitle);
const isDate = fieldConfig?.type === 'date';
if (title && isDate) {
const dateFormat = fieldConfig?.admin?.date?.displayFormat || dateFormatFromConfig;
title = formatDate(title, dateFormat, i18n?.language);
}
return title;
}; And use as you wish: admin: {
useAsTitle: ({doc}) => `${doc.firstname} ${doc.lastname}`
} But now I think I understand that I was terribly wrong. The |
Beta Was this translation helpful? Give feedback.
-
@matthijs166 based on https://github.com/payloadcms/payload/blob/master/CONTRIBUTING.md#before-starting I still wait for somebody from team to give us an answer. |
Beta Was this translation helpful? Give feedback.
-
The virtual field solution is kinda OK but then the rendered title in the admin won't auto-update when typing into the fields the title depends on. Is there any way to make that work? |
Beta Was this translation helpful? Give feedback.
-
I agree that this could be a nice feature. It could be a formatting syntax |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently
useAsTitle
property fromCollectionConfig
searches for the field in the collection. It would be useful to be able to format.For example, a person has
firstname
andlastname
properties, but for the title would be nice to have{{firstname}} {{lastname}}
. So, there are a few solutions:useAsTitle
to accept a string or a function which returns a stringuseAsTitle
will parse the variables, search for fields, and replace them.What do you think?
Beta Was this translation helpful? Give feedback.
All reactions