Like what we’re doing? Star us on GitHub!

How to add default value for blocks

Stupidism
4 months ago
1 1

I have a content field requires at least 1 block.
image

I want to save our editors' effort by providing a default value for it, but defaultValue is not working as I expected.

  • Stupidism
    4 months ago

    Found the answer myself.

    
                {
                  name: 'content',
                  label: false,
                  type: 'blocks',
                  minRows: 1,
                  maxRows: 1,
                  blocks: [
                      {
                        slug: 'content',
                        labels: {
                          singular: 'Content',
                          plural: 'Content Blocks',
                        },
                        fields: [
                          {
                            name: 'richText',
                            type: 'richText',
                          }
                        ]
                      }
                  ],
                  defaultValue: [
                    {
                      richText: [{ children: [{ text: '' }] }],
                      blockType: 'content',
                    },
                  ],
                },
    

    It's really interesting that writing/asking the question itself can give me the answer.

Open the post
Continue the discussion in GitHub
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More