Switch toggle – Panel field

Switch Field for Kirby CMS

I created a very simple switch toggle field for the panel. It’s basically a simple single checkbox with an checked or not state (therefore this documentation applies). But with a lil CSS to make it look more fancy - or smooth. I just like it better for some things than a single checkbox or the existing radiobutton toggle option.

switch

And I thought, I’ll just share it. Documentation & Download: https://github.com/distantnative/field-switch

Usage

In your blueprint:

switchit:
  label: Switch
  type: switch
  text: You can turn me on and off
5 Likes

This looks very nice :slight_smile: Thank you a lot

1 Like

Nice, I’d thought about it as well, but thanks for doing it for us:)

1 Like

Useful! Thanks for sharing!

I just released an update of the Switch field and bumped up the version number to 1.0.0. As the version number suggests, this update changes a few things that can break current setups. You can find it at https://github.com/distantnative/field-switch

Main change is that I restructured the field to be delivered as it plugin (which requires Kirby 2.3 - currently in beta). It goes into site/plugins/field-switch/. The plugin then registers the field. With the new registry in Kirby 2.3, I believe all third-party elements like fields, snippets, blueprints etc. should also be delivered wrapped as a plugin. This enables us to keep only custom elements that are rather specific to the project in site/fields, site/tags etc.

Changlog

  • Restructured as Kirby plugin that registers the field (requires Kirby 2.3)
  • More precise plugin name field-switch (required renaming repository)
  • Leaner PHP code
  • Better scoping of CSS
  • Rewritten CSS as SCSS (using gulp)
  • Relicensed under MIT license
  • Improved README and package.json (works now with the Kirby CLI)
1 Like

Hey, im a beginner in kirby, and i defined my switch and cant get it to work in my template

in my blueprint:

headlineswitch:
label: Blue Header Text
type: switch
text: Blue or white

in my template i made it to just type true/false depending on the switch value like this:

if ($page->headlineswitch() == '1'){
    echo  "true";
}
else{
    echo "false";
}
exit();

and no matter if the switch is on/off in the panel it always prints false.

same thing happens if i change the if condition to true and with checkbox.

thanks for any help!

.

1 Like

You need to check if it is true, like this:

if ($page->headlineswitch() == 'true'){
    echo  "true";
}
else{
    echo "false";
}

There is a helper method called ->bool() that works with either format, so this is preferred:

if ($page->headlineswitch()->bool()){
    echo  "true";
}
else{
    echo "false";
}

The switch field saves true or false in the content file, not 1 or 0.

Thanks for those examples @texnixe.

I just tried both of your examples above and can only get the second version using bool() to behave correctly. Thought I’d mention in case this is useful for anyone else!

Thanks for pointing this out. There was a typo in Sonja’s post which I have now corrected. However the bool() method is preferred anyway as it is more robust.

Why is it gone! The toggle swith now only has a text indication. I would love to see a color change to green for better UI

You are replying to a seven-year-old post referring to Kirby 2 which has long reached end of life.

In Kirby 3 there is also a color change involved, not only text.