我正在探索 Payload CMS 项目的开发,该项目具有一组页面,每个页面都有一个“块”字段。我的目标是根据所选页面类型(例如“主页”或“联系人”)配置不同的阻止选项。这种级别的定制在 Payload CMS 中可行吗?如果是的话,你能指导我实现这个功能吗?
我尝试使用组件中的“条件”选项,但似乎无法修改块中的选项。
Payload 目前不支持此功能。您的配置必须按页面类型分割块。例如:
export const Pages: CollectionConfig = {
slug: 'pages',
fields: [
// ... your other fields
{
type: 'type',
name: 'type',
options: ['contact', 'home', 'post'],
},
{
name: 'contactBlocks',
type: 'blocks'
blocks: [ /* all the block types you need for contact specifically */],
admin: {
condition: ({ type }) => type === 'contact',
},
},
{
name: 'homeBlocks',
type: 'blocks'
blocks: [ /* all the block types you need for home specifically */],
admin: {
condition: ({ type }) => type === 'home',
},
},
{
name: 'blocks',
type: 'blocks'
blocks: [ /* all the block types of any other page */],
admin: {
// if it doesn't contain 'contact' or 'home' specifically you can show the default blocks
condition: ({ type }) => !['contact', 'home'].includes(data.type),
},
},
],
// ... the rest of the collection config
}
然后前端需要根据类型渲染可用的块。
我有同样的问题,我在 github 上开启了关于这个主题的讨论: https://github.com/payloadcms/payload/discussions/4668