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.

Is it possible to add extra fields to the user collection using a plugin?

default discord avatar
bruvimtired8 months ago
11

Would it be possible to add an extra field to the slug defined as

admin.user

in the payload config from a plugin?


I want to add a checkbox into the user collection form the plugin.

  • default discord avatar
    notchr8 months ago

    You can



    So its a User collection?

  • default discord avatar
    bruvimtired8 months ago

    @notchris Yeah

  • default discord avatar
    notchr8 months ago

    also



    Wait so this is a checkbox field



    on the user collection

  • default discord avatar
    bruvimtired8 months ago

    Yeah, so when you install the plugin I want it to add a checkbox field to the user collection

  • default discord avatar
    notchr8 months ago
    import type { Plugin } from 'payload/config'
    
    export const addCheckboxExamplePlugin =
      (pluginOptions: {}): Plugin =>
      incomingConfig => {
        let config = { ...incomingConfig }
    
        config.collections = [...(config.collections || [])]
    
        if (config.collections !== undefined) {
          const userCollection = config.collections.find((collection) => collection.slug === 'User')
          userCollection.fields = [
            ...userCollection.fields,
            {
                name: 'exampleField',
                type: 'checkbox', // required
                label: 'Click me to see fanciness',
                defaultValue: false,
            }
          ]
        }
    
        config.onInit = async payload => {
          if (incomingConfig.onInit) await incomingConfig.onInit(payload)
        }
    
        return config
      }


    Something like that



    You probs want to add a check to make sure

    const userCollection = config.collections.find((collection) => collection.slug === 'User')

    exists

  • default discord avatar
    bruvimtired8 months ago

    I could maybe just use

    incomingConfig.admin.user.slug

    instead of 'User'?

  • default discord avatar
    notchr8 months ago

    like



    if (userCollection) {...}


    well, if you can check to make sure incomingConfig has the slug you need then yes

  • default discord avatar
    bruvimtired8 months ago

    Okay thanks @notchris let me try this

  • default discord avatar
    notchr8 months ago

    Let me know how it goes!

  • default discord avatar
    bruvimtired8 months ago

    @notchris It worked! thanks



    if (config.collections !== undefined) {
      const userCollection = config.collections.find(collection => collection.slug === incomingConfig.admin?.user);
      if (userCollection) {
        userCollection.fields = [
          ...userCollection.fields,
          {
            name: "exampleField",
            type: "checkbox", // required
            label: "Click me to see fanciness",
            defaultValue: false,
          },
        ];
      }
    }
  • default discord avatar
    notchr8 months ago

    Nicely done!!

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.