# Blocks

A visual editor for long-form text and modular pages

****

- Read more: <https://getkirby.com/docs/guide/page-builder>
- Source: <https://github.com/getkirby/kirby/tree/5.5.3/src/Form/Field/BlocksField.php>

****

The Blocks field is the perfect solution for complex single-column layouts and long-form text.

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=1416&amp;height=2622&amp;enlarge=0"><img alt="" class="rounded" height="1711" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=924&amp;height=1711&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=462&amp;height=855&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=690&amp;height=1278&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=924&amp;height=1711&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=1380&amp;height=2555&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/b252439efe-1717846665/blocks.png?width=1416&amp;height=2622&amp;enlarge=0 1848w" width="924"></a></figure>

## Blueprint setup

You can add the Blocks field to any fields section in your Panel like this:

```yaml
fields:
  text:
    type: blocks
```

## Field properties

Property | Type | Default | Description
- | - | - | - |
autofocus | `bool` | - | Sets the focus on this field when the form loads. Only the first field with this property gets focus.
default | `array` | - | Default value for the field, which will be used when a page/file/user is created
disabled | `bool` | - | If true, the field is no longer editable and will not be saved
empty | `string` | - | Customize the default text when the blocks field is empty
fieldsets | `array` | - | Defines the allowed block types in the blocks field. See below.
help | | - | Optional help text below the field
label | | - | The field label can be set as string or associative array with translations
max | `int` | - | Maximum number of allowed blocks
min | `int` | - | Minimum number of required blocks
pretty | `bool` | `false` | Saves pretty printed JSON in text files
required | `bool` | `false` | If true, the field has to be filled in correctly to be saved.
translate | `bool` | `true` | If false, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.
when |  | - | Conditions when the field will be shown
width | `string` | `1/1` | The width of the field in the field grid. Available widths: `1/1`, `1/2`, `1/3`, `1/4`, `2/3`, `3/4`

## Defining Fieldsets

By default, the block selector shows a single list of available block types in alphabetical order.

```yaml
fields:
  text:
    type: blocks
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=476&amp;height=425&amp;enlarge=0"><img alt="" class="rounded" height="425" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=476&amp;height=425&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=462&amp;height=412&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=476&amp;height=425&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=476&amp;height=425&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=476&amp;height=425&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/28f56c643c-1717846665/single.png?width=476&amp;height=425&amp;enlarge=0 1848w" width="476"></a></figure>

However, you can fully customize the selector for new block types with sorted block types and groups.

To change the order of available blocks, list them manually with the `fieldsets` option:

```yaml
fields:
  text:
    type: blocks
    fieldsets:
      - heading
      - text
      - list
      - image
      - gallery
      - video
      - code
      - markdown
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=478&amp;height=387&amp;enlarge=0"><img alt="" class="rounded" height="387" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=478&amp;height=387&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=462&amp;height=374&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=478&amp;height=387&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=478&amp;height=387&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=478&amp;height=387&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/a695cab430-1717846665/single-sorted.png?width=478&amp;height=387&amp;enlarge=0 1848w" width="478"></a></figure>

### Groups

You can group block types in the block selector for a better overview of the available options. This is particularly useful when your blocks lists gets longer once your start creating your own custom blocks.

To create such groups, use the Blocks field's generic "group" block type:

```yaml
fields:
  text:
    type: blocks
    fieldsets:
      text:
        label: Text
        type: group
        fieldsets:
          - heading
          - text
          - list
      media:
        label: Media
        type: group
        fieldsets:
          - image
          - video
      code:
        label: Code
        type: group
        fieldsets:
          - code
          - markdown
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=478&amp;height=439&amp;enlarge=0"><img alt="" class="rounded" height="439" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=478&amp;height=439&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=462&amp;height=424&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=478&amp;height=439&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=478&amp;height=439&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=478&amp;height=439&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/8977b7153c-1717846665/groups.png?width=478&amp;height=439&amp;enlarge=0 1848w" width="478"></a></figure>

### Closed groups

By default, all groups are open. With very long list, you might want to close groups of blocks that are used less frequently with the `open: false` option:

```yaml
fields:
  text:
    type: blocks
    fieldsets:
      text:
        label: Text
        type: group
        fieldsets:
          - heading
          - text
          - list
      media:
        label: Media
        type: group
        fieldsets:
          - image
          - video
      code:
        label: Code
        type: group
        open: false
        fieldsets:
          - code
          - markdown
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=476&amp;height=349&amp;enlarge=0"><img alt="" class="rounded" height="349" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=476&amp;height=349&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=462&amp;height=339&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=476&amp;height=349&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=476&amp;height=349&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=476&amp;height=349&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/blocks/5596d0c141-1717846665/closed-groups.png?width=476&amp;height=349&amp;enlarge=0 1848w" width="476"></a></figure>

## Default values

You can set default blocks/values for blocks fields which will prepopulate the field using the `default` prop. The `type` property is required, but default blocks can be empty or filled with example content.

### Empty default block

```yaml
fields:
  text:
    type: blocks
    default:
      - type: text
```

### Default block with content

```yaml
fields:
  text:
    type: blocks
    default:
      - type: text
        content:
          text: Write something great…
```

### Multiple block types

You can also set multiple default block entries:

```yaml
fields:
  text:
    type: blocks
    default:
      - type: heading
        content:
          level: h2
          text: A heading
      - type: text
        content:
          text: Write something great…
```
## Block types

<nav class="auto-fill mb-12 text-sm" style="--min: 20rem; --gap: var(--spacing-1)">
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/code" style="text-decoration: none">
		<strong class="block">code</strong>
		An inline code block for code snippets and examples.	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/gallery" style="text-decoration: none">
		<strong class="block">gallery</strong>
		A list of images to create galleries of all sorts	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/heading" style="text-decoration: none">
		<strong class="block">heading</strong>
		Headline from &lt;h1&gt; to &lt;h6&gt;	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/image" style="text-decoration: none">
		<strong class="block">image</strong>
		A single image	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/line" style="text-decoration: none">
		<strong class="block">line</strong>
		A separating line (hr)	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/list" style="text-decoration: none">
		<strong class="block">list</strong>
		Ordered and unordered lists	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/markdown" style="text-decoration: none">
		<strong class="block">markdown</strong>
		A plaintext HTML/markdown/kirbytext block. This is perfect to combine WYSIWYG content with custom HTML, Kirbytext or Markdown.	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/quote" style="text-decoration: none">
		<strong class="block">quote</strong>
		A quote with optional citation	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/table" style="text-decoration: none">
		<strong class="block">table</strong>
		A table block based on the structure field	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/text" style="text-decoration: none">
		<strong class="block">text</strong>
		A simple text block with multiple paragraphs	</a>
		<a class="block p-3 bg-light" href="https://getkirby.com/docs/reference/panel/blocks/video" style="text-decoration: none">
		<strong class="block">video</strong>
		A Youtube or Vimeo video embed	</a>
	</nav>

## Custom block types

Kirby's built-in blocktypes are not enough? Create your own! Check out our extensive documentation on custom block types, from basic to advanced:

- <a href="https://getkirby.com/docs/guide/page-builder/custom-blocks">Custom blocks</a>
- <a href="https://getkirby.com/docs/reference/plugins/extensions/blocks">Custom block types</a>
- <a href="https://getkirby.com/docs/guide/page-builder/complex-custom-block">Audio block with preview</a>
- <a href="https://getkirby.com/docs/guide/page-builder/block-examples">Block factory: Creating your own blocks collection</a>
- <a href="https://getkirby.com/docs/cookbook/content-structure/nested-blocks">Nested blocks</a>

## Block preview type `fields`

Instead of manually creating previews for blocks types, you can use the `fields` preview type to display the fields together with their content.


```yaml
fields:
  blocks:
    type: blocks
    fieldsets:
      demo:
        wysiwyg: true
        preview: fields
        tabs:
          main:
            fields:
              title:
                type: text
              subtitle:
                type: text
              description:
                type: writer
          settings:
            label: Settings
            fields: # ...
```

## Extending core blocks

From modifying the output to how they are previewed in the Panel, you can also overwrite Kirby's built-in block types:

- <a href="https://getkirby.com/docs/guide/page-builder/customizing-core-blocks">Extending core blocks</a>
- <a href="https://getkirby.com/docs/reference/panel/blocks">Block types</a>

<h2 id="blocks-in-your-templates"><a href="#blocks-in-your-templates" tabindex="-1">Blocks in your templates</a></h2>
<p>If you don't want to care about the HTML for each individual block, you can echo the entire blocks collection to render all blocks.</p>
<figure class="code">
<pre><code class="language-php">&lt;?= $page-&gt;myBlocksField()-&gt;toBlocks() ?&gt;</code></pre>
</figure>
<h3 id="blocks-in-your-templates__looping-through-blocks"><a href="#blocks-in-your-templates__looping-through-blocks" tabindex="-1">Looping through blocks</a></h3>
<p>Looping through blocks to control their HTML can be very powerful. You can assign custom CSS classes, IDs for links and more.</p>
<p>You don't need to render the HTML for each individual block in the loop though. You can wrap the block with your custom HTML and then echo the <code class="type">$block</code> object to render the matching block snippet.</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;myBlocksField()-&gt;toBlocks() as $block): ?&gt;
&lt;div id="&lt;?= $block-&gt;id() ?&gt;" class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
  &lt;?= $block ?&gt;
&lt;/div&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<h3 id="blocks-in-your-templates__manually-loading-snippets"><a href="#blocks-in-your-templates__manually-loading-snippets" tabindex="-1">Manually loading snippets</a></h3>
<p>Sometimes you might wish to customize the way block snippets are loaded. Maybe you want to inject more snippet variables.</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;myBlocksField()-&gt;toBlocks() as $block): ?&gt;
&lt;div id="&lt;?= $block-&gt;id() ?&gt;" class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
  &lt;?php snippet('blocks/' . $block-&gt;type(), [
    'block' =&gt; $block,
    'theme' =&gt; 'dark'
  ]) ?&gt;
&lt;/div&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<p>… or load snippets from a different location …</p>
<figure class="code">
<pre><code class="language-php">&lt;?php foreach ($page-&gt;myBlocksField()-&gt;toBlocks() as $block): ?&gt;
&lt;div id="&lt;?= $block-&gt;id() ?&gt;" class="block block-type-&lt;?= $block-&gt;type() ?&gt;"&gt;
  &lt;?php snippet('blocks/custom/' . $block-&gt;type(), [
    'block' =&gt; $block,
    'theme' =&gt; 'dark'
  ]) ?&gt;
&lt;/div&gt;
&lt;?php endforeach ?&gt;</code></pre>
</figure>
<details class="since list-none" open="1">
<summary >
	Since <a href="https://github.com/getkirby/kirby/releases/tag/5.5.0">5.5.0</a></summary>
<div>
	<h3 id="blocks-in-your-templates__debugging"><a href="#blocks-in-your-templates__debugging" tabindex="-1">Debugging</a></h3>
<p>Bugs in your blog snippets will throw an exception in debug mode, but will otherwise be swallowed.</p></div>
</details>

## Configuration

You can configure the default setup of your Blocks field in your config.php

```php "/site/config/config.php"
<?php

return [
  'blocks' => [
    'fieldsets' => [
      'text' => [
        'label' => 'Text',
        'type' => 'group',
        'fieldsets' => [
          'text',
          'heading'
        ]
      ],
      'media' => [
        'label' => 'Media',
        'type' => 'group',
        'fieldsets' => [
          'image',
          'video'
        ]
      ]
    ]
  ]
];
```

## Further reading

- <a href="https://getkirby.com/docs/reference/panel/blocks">Block types</a>
- <a href="https://getkirby.com/docs/reference/objects/cms/block">Block methods</a>
- <a href="https://getkirby.com/docs/reference/objects/cms/blocks">Blocks collection methods</a>