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.

Blocks Field

The Blocks Field is incredibly powerful, storing an array of objects based on the fields that your define, where each item in the array is a "block" with its own unique schema.

Blocks are a great way to create a flexible content model that can be used to build a wide variety of content types, including:

  • A layout builder tool that grants editors to design highly customizable page or post layouts. Blocks could include configs such as Quote, CallToAction, Slider, Content, Gallery, or others.
  • A form builder tool where available block configs might be Text, Select, or Checkbox.
  • Virtual event agenda "timeslots" where a timeslot could either be a Break, a Presentation, or a BreakoutSession.
Admin Panel screenshot of add Blocks drawer view
Admin Panel screenshot of add Blocks drawer view

To add a Blocks Field, set the type to blocks in your Field Config:

1
import type { Field } from 'payload'
2
3
export const MyBlocksField: Field = {
4
// ...
5
type: 'blocks',
6
blocks: [
7
// ...
8
],
9
}

Config Options

OptionDescription
name *To be used as the property name when stored and retrieved from the database. More
labelText used as the heading in the Admin Panel or an object with keys for each language. Auto-generated from name if not defined.
blocks *Array of block configs to be made available to this field.
validateProvide a custom validation function that will be executed on both the Admin Panel and the backend. More
minRowsA number for the fewest allowed items during validation when a value is present.
maxRowsA number for the most allowed items during validation when a value is present.
saveToJWTIf this field is top-level and nested in a config supporting Authentication, include its data in the user JWT.
hooksProvide Field Hooks to control logic for this field. More details.
accessProvide Field Access Control to denote what users can see and do with this field's data. More details.
hiddenRestrict this field's visibility from all APIs entirely. Will still be saved to the database, but will not appear in any API response or the Admin Panel.
defaultValueProvide an array of block data to be used for this field's default value. More
localizedEnable localization for this field. Requires localization to be enabled in the Base config. If enabled, a separate, localized set of all data within this field will be kept, so there is no need to specify each nested field as localized.
uniqueEnforce that each entry in the Collection has a unique value for this field.
labelsCustomize the block row labels appearing in the Admin dashboard.
adminAdmin-specific configuration. More details.
customExtension point for adding custom data (e.g. for plugins)
typescriptSchemaOverride field type generation with providing a JSON schema
virtualProvide true to disable field in the database. See Virtual Fields

* An asterisk denotes that a property is required.

Admin Options

The customize the appearance and behavior of the Blocks Field in the Admin Panel, you can use the admin option:

1
import type { Field } from 'payload'
2
3
export const MyBlocksField: Field = {
4
// ...
5
admin: {
6
// ...
7
},
8
}

The Blocks Field inherits all of the default options from the base Field Admin Config, plus the following additional options:

OptionDescription
initCollapsedSet the initial collapsed state
isSortableDisable order sorting by setting this value to false

Customizing the way your block is rendered in Lexical

If you're using this block within the Lexical editor, you can also customize how the block is rendered in the Lexical editor itself by specifying custom components.

  • admin.components.Label - pass a custom React component here to customize the way that the label is rendered for this block
  • admin.components.Block - pass a component here to completely override the way the block is rendered in Lexical with your own component

This is super handy if you'd like to present your editors with a very deliberate and nicely designed block "preview" right in your rich text.

For example, if you have a gallery block, you might want to actually render the gallery of images directly in your Lexical block. With the admin.components.Block property, you can do exactly that!

To import these utility components for one of your custom blocks, you can import the following:

1
import {
2
// Edit block buttons (choose the one that corresponds to your usage)
3
// When clicked, this will open a drawer with your block's fields
4
// so your editors can edit them
5
InlineBlockEditButton,
6
BlockEditButton,
7
8
// Buttons that will remove this block from Lexical
9
// (choose the one that corresponds to your usage)
10
InlineBlockRemoveButton,
11
BlockRemoveButton,
12
13
// The label that should be rendered for an inline block
14
InlineBlockLabel,
15
16
// The default "container" that is rendered for an inline block
17
// if you want to re-use it
18
InlineBlockContainer,
19
20
// The default "collapsible" UI that is rendered for a regular block
21
// if you want to re-use it
22
BlockCollapsible,
23
24
} from '@payloadcms/richtext-lexical/client'

Block Configs

Blocks are defined as separate configs of their own.

OptionDescription
slug *Identifier for this block type. Will be saved on each block as the blockType property.
fields *Array of fields to be stored in this block.
labelsCustomize the block labels that appear in the Admin dashboard. Auto-generated from slug if not defined.
imageURLProvide a custom image thumbnail to help editors identify this block in the Admin UI.
imageAltTextCustomize this block's image thumbnail alt text.
interfaceNameCreate a top level, reusable Typescript interface & GraphQL type.
graphQL.singularNameText to use for the GraphQL schema name. Auto-generated from slug if not defined. NOTE: this is set for deprecation, prefer interfaceName.
dbNameCustom table name for this block type when using SQL Database Adapter (Postgres). Auto-generated from slug if not defined.
customExtension point for adding custom data (e.g. for plugins)

Auto-generated data per block

In addition to the field data that you define on each block, Payload will store two additional properties on each block:

blockType

The blockType is saved as the slug of the block that has been selected.

blockName

The Admin Panel provides each block with a blockName field which optionally allows editors to label their blocks for better editability and readability.

Example

collections/ExampleCollection.js

1
import { Block, CollectionConfig } from 'payload'
2
3
const QuoteBlock: Block = {
4
slug: 'Quote', // required
5
imageURL: 'https://google.com/path/to/image.jpg',
6
imageAltText: 'A nice thumbnail image to show what this block looks like',
7
interfaceName: 'QuoteBlock', // optional
8
fields: [
9
// required
10
{
11
name: 'quoteHeader',
12
type: 'text',
13
required: true,
14
},
15
{
16
name: 'quoteText',
17
type: 'text',
18
},
19
],
20
}
21
22
export const ExampleCollection: CollectionConfig = {
23
slug: 'example-collection',
24
fields: [
25
{
26
name: 'layout', // required
27
type: 'blocks', // required
28
minRows: 1,
29
maxRows: 20,
30
blocks: [
31
// required
32
QuoteBlock,
33
],
34
},
35
],
36
}

TypeScript

As you build your own Block configs, you might want to store them in separate files but retain typing accordingly. To do so, you can import and use Payload's Block type:

1
import type { Block } from 'payload'
Next

Checkbox Field