Hey there! Just a few days back I found the PayloadCMS and it seems like a nice tool - I started working on a project, but I'm clearly not very versed in it yet.
Now, I do have a problem regarding the select functionality - the situation is the following. I have the following data structure:
- Sellers: id (uuid), name (string), ...
- Products: id (uuid), name (string), ...
- SellerProducts: id (uuid), seller (relationship), product (relationship), ...
- SellerProductPromotions: id (uuid), sellerProduct (relationship), ...
Now, if I want to create a new SellerProductPromotions entry, the autocompleted select will by default return the ID for SellerProducts for the label, but that's clearly not viable.
How would I approach that? Seems that admin.useAsTitle only accepts a string, but I would need something like "(entry) =>
${entry.product.name} (${entry.seller.name})
" (including the filter search for that). Is that possible to do with PayloadCMS?
Hi
@269130158982561794- you could create a virtual field which is a common solution we use for creating dynamic titles. Read more about them here
https://payloadcms.com/blog/learn-how-virtual-fields-can-help-solve-common-cms-challengesEssentially you could add another field that combines your entry.product.name and entry.seller.name and use that as your title. You can also add a beforeChange hook to remove the value from your database so it is fully virtual.
{
name: 'title',
type: 'text',
admin: {
hidden: true, // hides the field from the admin panel
},
hooks: {
beforeChange: [
({ siblingData }) => {
// ensures data is not stored in DB
delete siblingData['title']
}
],
afterRead: [
({ data }) => {
return `${data.product.name} ${data.seller.name}`;
}
],
},
}
Something along the lines of this 👆 Hope this helps!
Oh wow, that was quick! Seems like exactly what I'm looking for - thank you!
Star
Discord
online
Get dedicated engineering support directly from the Payload team.