Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Latest commit

 

History

History
58 lines (42 loc) · 1.67 KB

lightswitch-fields.md

File metadata and controls

58 lines (42 loc) · 1.67 KB

Lightswitch Fields

Lightswitch fields give you a simple toggle input for times when all you need is a “Yes” or “No” answer.

Templating

Querying Elements with Lightswitch Fields

When querying for elements that have a Lightswitch field, you can filter the results based on the Lightswitch field data using a query param named after your field’s handle.

Possible values include:

Value Fetches elements…
true or ':notempty:' with an enabled Lightswitch value.
false or ':empty:' with a disabled Lightswitch value.
{# Fetch entries with the Lightswitch field enabled #}
{% set entries = craft.entries()
    .<FieldHandle>(true)
    .all() %}

::: tip Any elements that don’t have an explicit Lightswitch value set will be treated as if they have the default field value, per the field settings. :::

Working with Lightswitch Field Data

If you have an element with a Lightswitch field in your template, you can access its data using your Lightswitch field’s handle:

{% if entry.<FieldHandle> %}
    <p>I'm on!</p>
{% else %}
    <p>I'm off.</p>
{% endif %}

::: tip If the element doesn’t have an explicit Lightswitch field value yet, the field’s default value will be returned. :::

Saving Lightswitch Fields in Entry Forms

If you have an entry form that needs to contain a Lightswitch field, you can use this template as a starting point:

{{ hiddenInput('fields[<FieldHandle>]', '') }}

{{ tag('input', {
  type: 'checkbox',
  name: 'fields[<FieldHandle>]',
  value: '1',
  checked: (entry.<FieldHandle> ?? false) ? true : false,
}) }}