Skip to content

Kirby 6.0.0

Structure

Structured data input, which stores data as YAML.

Field options

Name Type Default Description
autofocus bool false Sets the focus on this field when the form loads. Only the first field with this option gets focus
batch bool false Show/hide the batch select interface
columns array no default value Columns definition for the table. false hides a column, a string sets its label.
default mixed null Default value for the field, which will be used when a page/file/user is created
disabled bool false If true, the field is no longer editable
duplicate bool true Allow duplicating items in the field
empty arrayorstring null Sets the text for the empty state box
fields array [] Fields setup for the form. Works just like fields in regular forms.
help arrayorstring null Optional help text below the field
label arrayorstring no default value The field label can be set as string or associative array with translations
limit int null The number of entries that will be displayed on a single page. Afterwards pagination kicks in.
max int null Sets the maximum number of allowed items in the field
min int null Sets the minimum number of required items in the field
prepend bool false If activated, new items will be added at the start
required bool false If true, the field has to be filled in correctly to be saved.
sortBy string null Sorts the entries by the given field and order (i.e. title desc) Drag & drop is disabled in this case
sortable bool true If true, entries are sortable via drag & drop
translate bool true Should the field be translatable?
validate arrayorstring null Additional validator rules for the value, e.g. email or ['minLength' => 3]
when array null 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

The structure field makes it possible to add multiple complex entries to a field, which will be stored as YAML. A typical use case would be a list of addresses, team members or a restaurant menu.

Example

fields:
  addresses:
    label: Addresses
    type: structure
    fields:
      street:
        label: Street
        type: text
      zip:
        label: ZIP
        type: text
      city:
        label: City
        type: text

Such a structure will be stored in the content file like this:

addresses:
-
  street: Rue de WTF 17
  zip:    1112
  city:   Monaco
-
  street: 1212 Broadway
  zip:    4321
  city:   New York
-
  street: At the beach
  zip:    9999
  city:   The capitol of the Bahamas

If you need to store a single object instead of a list of objects, take a look at the object field.

Fields

You can define any number of fields and use the same field types listed:

Each field value is stored like in a normal Kirby content file. This means that all field types are supported, however all of their values are converted to strings. For most fields there is no difference, but fields with structured data will not be nested inside the same YAML data structure. This ensures broad compatibility.

Default values

You can set default values for structure fields which will prepopulate the field:

fields:
  emails:
    label: Emails
    type: structure
    default:
      - email: bastian@getkirby.com
      - email: lukas@getkirby.com
      - email: nico@getkirby.com
    fields:
      email:
        label: Email
        type: email

Table columns

You can define the columns that are shown in the table. This is especially useful if you have a lot of fields in your structure and you don't want to show them all on first sight, but still keep them editable. Columns can also change the text alignment, set a custom width and define a before and after text that will be prepended or appended to the value.

Option Value Description
width any fraction, e.g. 1/2, 1/3, 1/4, 2/3, 3/4 Set width of column
align left, center, right Set text alignment
before string Set text to prepend value
after string Set text to append value
mobile true Set visible in mobile view

Example

fields:
  holidays:
    type: structure
    columns:
      title:
        width: 1/4
      images:
        width: 1/2
      price:
        width: 1/4
        align: right
        after: "USD"
    fields:
      title:
        type: text
      images:
        type: filepicker
      description:
        type: textarea
      price:
        type: number

Structure columns can be set to any fraction and will be automatically calculated into the right width.

fields:
  mystructure:
    label: Structure
    type: structure
    columns:
      title:
        width: 3/5
      images:
        width: 1/5
      price:
        width: 1/10
    fields:
      title:
        label: A
        type: text
      images:
        label: B
        type: text
      price:
        label: C
        type: text

You can also define columns without additional options if you want to use the defaults. You can still combine this with options for other columns:

fields:
  holidays:
    type: structure
    columns:
      title: true
      images: true
      price:
        align: right
        after: "USD"
    fields:
      title:
        type: text
      images:
        type: filepicker
      description:
        type: textarea
      price:
        type: number

Hide toggle field text in preview

When using toggle fields within structures, displaying the field's text in the preview can sometimes look cluttered. You can hide the text with the text option in the columns' definition.

fields:
  structure:
    columns:
      a:
        text: false
      b:
        text: false
    fields:
      a:
        type: toggle
      b:
        type: toggle
        text: this is some long text

Set columns to visible on mobile

The mobile option keeps cells in responsive views. If none of the columns are set to mobile, only the first column will be visible on mobile.

fields:
  structure:
    columns:
      image:
        type: image
      title:
        type: text
        mobile: true
      date:
        type: date
        mobile: true
    fields:
      image:
        type: filepicker
      title:
        type: text
      date:
        type: date

Preview of fields in the table

The structure field tries to create the best possible preview for the field in its table view. If you are using fields provided by a plugin in the structure field, the preview of these can be customized with a field preview extension.

Batch delete

You can activate the batch delete mode for structure fields by using the batch option in your blueprint:

fields:
  structure:
    batch: true

How to use in templates/snippets

To access a structure field in your templates, you can use the yaml() and toStructure() methods.

Let's say we wanted to render the holidays structure field example from above in a template:

<?php
// using the `toStructure()` method, we create a structure collection
$items = $page->holidays()->toStructure();
// we can then loop through the entries and render the individual fields
foreach ($items as $item): ?>
  <h2><?= $item->title()->html() ?></h2>
  <?php foreach ($item->images()->toFiles() as $image): ?>
    <img src="<?= $image->crop(400)->url() ?>">
  <?php endforeach ?>
  <p><?= $item->price() ?></p>
<?php endforeach ?>

If you nest structure fields inside a structure field, you have to call the toStructure() method on the nested fields as well and then loop through the nested items like above.

Update programmatically

To change the field value from PHP, pass an array of entries to $page->update(). Each entry is an array with the names of the structure fields as keys:

$page->update([
  'addresses' => [
    [
      'street' => 'Main Street 1',
      'zip'    => '12345',
      'city'   => 'Springfield'
    ],
    [
      'street' => 'Second Street 2',
      'zip'    => '67890',
      'city'   => 'Shelbyville'
    ]
  ]
]);

You can also pass the entries of another structure field, e.g. $otherPage->addresses()->toStructure().

To add an entry to the existing ones, pass a callback:

$page->update([
  'addresses' => fn ($addresses) => [
    ...$addresses,
    [
      'street' => 'Third Street 3',
      'zip'    => '13579',
      'city'   => 'Capital City'
    ]
  ]
]);

More information

Structured field content