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.

Prefill relationship field from join field

default discord avatar
mikecebul7 months ago
2

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?

  • default discord avatar
    mariovalney7 months ago

    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)

  • default discord avatar
    mikecebul7 months ago

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.