Skip to content

Kirby 6.0.0

Object

Structured object input, which stores properties 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
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
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
required bool false If true, the field has to be filled in correctly to be saved.
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 object field allows to create data objects. This is handy for more complex settings, isolated entities or nested data.

A typical example would be contact data for an author in a blog article, multiple layout settings for images in your template, custom settings for a theme, SEO configuration etc.

Example

fields:
  contact:
    type: object
    fields:
      photo:
        type: filepicker
      name:
        type: text
      email:
        type: email
      phone:
        type: tel

Such an object will be stored in the content file like this:

contact:
  photo:
    - file://hb38HvnQfm8HlQ6e
  name: Nandi Peters
  email: nandi@example.com
  phone: 1234 5678

If you need to store a list of multiple objects, take a look at the structure 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 structured fields will not be nested inside the same YAML data structure. This ensures broad compatibility.

Default values

You can set default values for an object field which will prepopulate the field when a new page is created.

fields:
  contact:
    type: object
    default:
      name: Nandi Peters
      email: nandi@example.com
      phone: 1234 5678
    fields:
      photo:
        type: filepicker
      name:
        type: text
      email:
        type: email
      phone:
        type: tel

How to use in templates/snippets

To access a object field in your templates, you can use the toObject() method.

Let's say we wanted to render the contact data from above in a template:

<?php if ($contact = $page->contact()->toObject()): ?>
<dl>
  <dt>Photo</dt>
  <dd><?= $contact->photo()?->toFile()->crop(200) ?></dd>
  <dt>Name</dt>
  <dd><?= $contact->name() ?></dd>
  <dt>Email</dt>
  <dd><?= $contact->email() ?></dd>
  <dt>Phone</dt>
  <dd><?= $contact->phone() ?></dd>
</dl>
<?php endif ?>

If you nest object fields inside another object field, block field or structure field, you have to call the toObject() method on the nested fields as well and then access the nested object properties like above.

Update programmatically

To change the field value from PHP, pass an array with the names of the object fields as keys to $page->update():

$page->update([
  'contact' => [
    'name'  => 'Jane Doe',
    'email' => 'jane@example.com',
    'phone' => '+49 123 456789'
  ]
]);