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.
You can
So its a User collection?
@notchris Yeah
also
Wait so this is a checkbox field
on the user collection
Yeah, so when you install the plugin I want it to add a checkbox field to the user collection
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
I could maybe just use
incomingConfig.admin.user.slug
instead of 'User'?
like
if (userCollection) {...}
well, if you can check to make sure incomingConfig has the slug you need then yes
Okay thanks @notchris let me try this
Let me know how it goes!
@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,
},
];
}
}
Nicely done!!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.