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/types'
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

* 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/types'
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

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