Blocks Field

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.

Blocks field in Payload admin panel Admin panel screenshot of a Blocks field type with Call to Action and Number block examples

Field config

OptionDescription
name *To be used as the property name when stored and retrieved from the database.
labelUsed as a heading in the Admin panel and to name the generated GraphQL type.
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
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.
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 the default field admin config for more details.

* An asterisk denotes that a property is required.

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. Also used to name corresponding GraphQL schema types.
imageURLProvide a custom image thumbnail to help editors identify this block in the Admin UI.
imageAltTextCustomize this block's image thumbnail alt text.

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

const QuoteBlock = {
slug: 'Quote', // required
imageURL: 'https://google.com/path/to/image.jpg',
imageAltText: 'A nice thumbnail image to show what this block looks like',
fields: [ // required
{
name: 'text',
label: 'Quote Text',
type: 'text',
required: true,
},
{
name: 'text',
label: 'Quotee',
type: 'text',
},
]
};
const ExampleCollection = {
slug: 'example-collection',
fields: [
{
name: 'layout', // required
type: 'blocks', // required
label: 'layout',
minRows: 1,
maxRows: 20,
labels: {
singular: 'Layout',
plural: 'Layouts',
},
blocks: [ // required
QuoteBlock
]
}
]
}
Next

Checkbox Field