Blocks Field

Admin panel screenshot of add Blocks drawer view
Admin panel screenshot of add Blocks drawer view

Example uses:

  • 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.

Field config

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-level hooks to control logic for this field. More
accessProvide field-level access control to denote what users can see and do with this field's data. More
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. See below for more detail.
customExtension point for adding custom data (e.g. for plugins)

* An asterisk denotes that a property is required.

Admin Config

In addition to the default field admin config, you can adjust the following properties:

OptionDescription
initCollapsedSet the initial collapsed state
isSortableDisable block 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/types'
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/types'
Next

Checkbox Field