If I click "add new" on a join field, the relationship field in that new record should be prefilled. Do I need to make a custom component to do this or is there a built-in way to get that context?
They are prefilled here.
Said that, I'm not sure is your use-case but I want to choose the parent only when creating outside JOIN Field.
So I've created a custom component:
{
name: 'name',
relationTo: 'slug',
label: 'Label',
type: 'relationship',
hasMany: false,
maxDepth: 2,
required: true,
admin: {
allowCreate: false,
allowEdit: false,
components: {
Field: '@components/(payload)/fields/relationship/create-only',
}
},
access: {
create: () => true,
update: () => false,
},
}Field component
create-only.tsx'use client'
import { RelationshipField, useDocumentInfo, useEditDepth } from '@payloadcms/ui'
import { mergeObjects } from '@utils/collections'
import type { RelationshipFieldClientComponent } from 'payload'
const RelationshipCreateOnlyField: RelationshipFieldClientComponent = (props) => {
const editDepth = useEditDepth();
const { isEditing } = useDocumentInfo();
const creatingOnTopLevel = ! isEditing && editDepth === 1;
const className = creatingOnTopLevel ? 'relationship-create-only-field' : 'relationship-create-only-field read-only-relationship';
props = mergeObjects(props, {readOnly: ! creatingOnTopLevel});
props = mergeObjects(props, {field: { admin: { className: `${(props.field.admin?.className || '')} ${className}` } }});
return <RelationshipField {...props} />
}
export default RelationshipCreateOnlyField;(mergeObject is only a personal util to merge configs)
Oh yeah, it is already prefilled. I didn't realize I was looking at a field that was missing a step in the workflow.
I have a client with drug test results. The drug test collection is joined to the client. That relationship field is prefilled. That drug test collection has a relationship field of my private-media collection. It's only there where the relationship field is not prefilled with the client.
Hopefully that makes sense. I think the relationship field in the private-media collection is necessary because it uploads to S3 and is only accessible via presigned urls allowed by access control
Star
Discord
online
Get dedicated engineering support directly from the Payload team.