Skip to content

Kirby 6.0.0

Toggle

Yes/no or on/off toggle

Field options

Name Type Default Description
after arrayorstring null Optional text that will be shown after the input
autofocus bool false Sets the focus on this field when the form loads. Only the first field with this option gets focus
before arrayorstring null Optional text that will be shown before the input
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
help arrayorstring null Optional help text below the field
icon string null Optional icon that will be shown at the end of 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.
text arrayorstring null Sets the text next to the toggle. The text can be a string or an array of two options. The first one is the negative text and the second one the positive. The text will automatically switch when the toggle is triggered.
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 toggle field is perfect for simple on/off or yes/no states.

Example

fields:
  toggle:
    label: Include in menu?
    type: toggle
    text:
      - "no"
      - "yes"

Simple toggle field translated

fields:
  toggle:
    label: Toggle
    type: toggle
    text:
        en: Include in menu?
        de: Im Menü anzeigen?

Toggle text

fields:
  toggle:
    label: Toggle
    type: toggle
    text:
        - Nope
        - Yay

Translated text

fields:
  toggle:
    label: Toggle
    type: toggle
    text:
        -
            en: Nope
            de: Och nö
        -
            en: Yay
            de: Juhu

The field content will either be 'true' or 'false'.

Text from query

fields:
  toggle:
    label: Toggle
    type: toggle
    text: "{{ page.toggleText }}"

How to use in templates/snippet

Convert to a proper boolean:

$bool = $page->toggle()->toBool();

Do something depending on field value:

<?php
if ($page->toggle()->toBool() === true) {
  // do this
} else {
  // do that
}

Update programmatically

To change the field value from PHP, pass true or false to $page->update():

$page->update([
  'toggle' => true
]);