Compound admin.useAsTitle

default discord avatar
hellboy124124last year
2

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?

  • discord user avatar
    jesschow
    last year

    Hi @hellboy124124 - 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-challenges

    Essentially 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!

  • default discord avatar
    hellboy124124last year

    Oh wow, that was quick! Seems like exactly what I'm looking for - thank you!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.