You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: <Sincever="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
+
```
38
77
39
78
### Working with Lightswitch Field Data
40
79
41
80
If you have an element with a lightswitch field in your template, you can access its value using the field’s handle:
42
81
43
82
::: code
44
83
```twig
45
-
{% if entry.myFieldHandle %}
84
+
{% if entry.myLightswitchField %}
46
85
<p>I’m on!</p>
47
86
{% else %}
48
87
<p>I’m off.</p>
49
88
{% endif %}
50
89
```
51
90
```php
52
-
if ($entry->myFieldHandle) {
91
+
if ($entry->myLightswitchField) {
53
92
// I’m on!
54
93
} else {
55
94
// I’m off.
@@ -62,12 +101,19 @@ if ($entry->myFieldHandle) {
62
101
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:
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