Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Create a field with a hook that combines the value of a block-level and relationship-level field.

default discord avatar
2me2youlast year
7

I have 3 collections.



Parent


Child


Grandchild



My Child collection has a name, and relationship field to parent. I'm trying to create a field in this collection that has a beforeChange hook to combine the Parent name with Child value field and this is what I'd like to use for useAsTitle. However, I am not able to get the value of Parent as it is only returning me the document id. I understand that the Relationship fields has a harcoded depth of 0 but is there another way to accomplish this use case?



For context, the reason why I'd like to do this is because it is possible for my Child collection to have the same name and I'd like my Grandchild collection to also have a relationship to my Child collection however it is difficult to identiffy what item should the user select because they have the same names so I'd like to include the Parent's name with the Child's name to identify each option in the relationship field uniquely user experience wise.

  • You could create another field (a text field) that you set the value of the two combined to, and use that as your useAsTitle field



    and use the beforeChange hook to determine the value and then set it programmatically, by appending it to the data you return in your beforeChange hook.



    You could also hide the field if you would like with

    admin.hidden

    or

    admin.components.Field: () => null
  • default discord avatar
    2me2youlast year

    This is what I'm doing with a text field and a relationship field. Unfortunately, I'm unable to get the actual value of the relationship field because it only returns the id in the beforeChange hook. Adding another text field to rewrite the value of the relationship field ao I can use that in my beforeChange hook instead seems counter intuitive.

  • You would need to use a payload.find to get the full doc. I know it seems counterintuitive, but what if you had 10 child>child>child>child and you wanted to use them all? Payload is not going to fetch every single child document because that would greatly impact performance and loading of data.

  • default discord avatar
    2me2youlast year

    Ah I see. So payload.find is something I can use in the beforeChange hook. Do I need to call the api or would I be able to find it using the param of beforeChange hook?

  • you can use req.payload.find within the beforeChange hook

  • default discord avatar
    2me2youlast year

    Cool! This is what I'm looking for. I'll try this. Thank you!

  • no problem!

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.