Skip to content

Kirby 3.9.8

Toggle

Yes/no or on/off toggle

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"

Field properties

Name Type Default Description
after Optional text that will be shown after the input
autofocus bool Sets the focus on this field when the form loads. Only the first field with this label gets
before Optional text that will be shown before the input
default Default value which will be saved when a new page/user/file is created
disabled bool If true, the field is no longer editable and will not be saved
help Optional help text below the field
icon string Optional icon that will be shown at the end of the field
label The field label can be set as string or associative array with translations
required bool If true, the field has to be filled in correctly to be saved.
text 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 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 (since 3.1.0)
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

Simple toggle field translated

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

Text toggle

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

Note: The values No and Yes will not work out of the box as the YAML parser will interpret them as booleans. You need to quote them as "No" and "Yes".

Text toggle translated

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'.

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
}