Skip to content

Commit 0ac538e

Browse files
committed
5.7: Lightswitch behavior, templates
1 parent cd7de44 commit 0ac538e

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

docs/5.x/reference/field-types/lightswitch.md

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ When [querying for elements](../../development/element-queries.md) that have a l
1212

1313
Possible values include:
1414

15-
| Value | Fetches elements…
16-
| - | -
17-
| `true` | with the switch _on_.
18-
| `false` | with the switch _off_.
15+
| Value | Fetches elements… |
16+
| --- | --- |
17+
| `true` | with the switch _on_. |
18+
| `false` | with the switch _off_. |
1919

2020
::: code
2121
```twig
@@ -32,24 +32,63 @@ $entries = \craft\elements\Entry::find()
3232
```
3333
:::
3434

35-
::: tip
36-
Any elements that don’t have a value set for a lightswitch field will be treated as if they have the default field value, per the field’s settings.
37-
:::
35+
Elements without a value for a lightswitch field (say, because they haven't been saved since the field was added) are treated as if they have the default field value. For example, querying against a field that defaults to _off_
36+
37+
```twig{3}
38+
{% set archive = craft.entries()
39+
.section('essays')
40+
.isFeatured(false)
41+
.all() %}
42+
```
43+
44+
…would return all posts with an explicit `false` value, or no saved value. This can result in unpredictable behavior when some elements in a set may not have the field. Suppose we want to gather content across two sections (featured “essays” as well as implicitly high-priority “bulletins”):
45+
46+
```twig{2}
47+
{% set headlines = craft.entries()
48+
.section(['essays', 'bulletins'])
49+
.isFeatured(true)
50+
.all() %}
51+
```
52+
53+
If only the `essays` section has an `isFeatured` lightswitch field, entries in the `bulletins` section are assumed to have the field’s default value (`false`), and are therefore excluded.
54+
55+
For more control over this behavior, you have two options: <Since ver="5.7.0" feature="Customizing null handling for lightswitch fields" />
56+
57+
- Pass each acceptable value, explicitly…
58+
59+
```twig{3}
60+
{% set headlines = craft.entries()
61+
.section(['essays', 'bulletins'])
62+
.isFeatured([true, null])
63+
.all() %}
64+
```
65+
66+
- Pass a hash with a `strict` key…
67+
68+
```twig{3-6}
69+
{% set headlines = craft.entries()
70+
.section(['essays', 'bulletins'])
71+
.isFeatured({
72+
value: true,
73+
strict: true,
74+
})
75+
.all() %}
76+
```
3877

3978
### Working with Lightswitch Field Data
4079

4180
If you have an element with a lightswitch field in your template, you can access its value using the field’s handle:
4281

4382
::: code
4483
```twig
45-
{% if entry.myFieldHandle %}
84+
{% if entry.myLightswitchField %}
4685
<p>I’m on!</p>
4786
{% else %}
4887
<p>I’m off.</p>
4988
{% endif %}
5089
```
5190
```php
52-
if ($entry->myFieldHandle) {
91+
if ($entry->myLightswitchField) {
5392
// I’m on!
5493
} else {
5594
// I’m off.
@@ -62,12 +101,19 @@ if ($entry->myFieldHandle) {
62101
In an element or [entry form](kb:entry-form) that needs to save a value to a lightswitch field, you can use this template as a starting point:
63102

64103
```twig
65-
{{ hiddenInput('fields[myFieldHandle]', '') }}
104+
{{ hiddenInput('fields[myLightswitchField]', '') }}
66105
67106
{{ tag('input', {
68107
type: 'checkbox',
69-
name: 'fields[myFieldHandle]',
108+
name: 'fields[myLightswitchField]',
70109
value: '1',
71-
checked: (entry.myFieldHandle ?? false) ? true : false,
110+
checked: (entry.myLightswitchField ?? false) ? true : false,
111+
id: 'my-checkbox-input',
72112
}) }}
113+
114+
<label for="my-checkbox-input">Opt In</label>
73115
```
116+
117+
Your input’s `value` can be any “truthy” looking value; Craft only stores a boolean. If you wish to capture one or more explicit values, consider using a [radio](radio-buttons.md), [checkboxes](checkboxes.md), or [multi-select](multi-select.md) field.
118+
119+
The `hidden` input is necessary to tell Craft in the POST request that the field’s value should be updated. If you do not send a value under a custom field handle (`fields[myLightswitchField]` in the example), Craft assumes you _do not_ wish to update it. When the checkbox is ticked, its `value` is sent (`1` in the example) in place of an empty string.

0 commit comments

Comments
 (0)