FieldtypeToggle
A yes/no/other toggle with an optional "unknown" (no-selection) state
Unlike FieldtypeCheckbox,
Toggle can distinguish between a "no" selection and no selection at all.
int|string — one of: 1 (yes), 0 (no), 2 (other, if enabled), or '' (unknown / no selection).
Constants: Fieldtype (1), ::valueNo (0), ::valueOther (2), ::valueUnknown ('').
// Get with output formatting OFF (always int or ''):
$page->of(false);
$page->toggle_field; // 1, 0, 2, or ''
// Get with output formatting ON (type depends on formatType field setting):
$page->of(true);
$page->toggle_field; // int, bool, or string label
// Set — accepts int, keyword string, or bool:
$page->toggle_field = 1; // yes
$page->toggle_field = 0; // no
$page->toggle_field = 2; // other (if enabled for field)
$page->toggle_field = ''; // unknown / no-selection
$page->toggle_field = 'yes'; // same as 1
$page->toggle_field = 'no'; // same as 0
$page->toggle_field = 'unknown'; // same as ''
$page->toggle_field = true; // same as 1
$page->toggle_field = false; // same as 0
$page->save('toggle_field');// Yes selected
$pages->find('toggle_field=1');
$pages->find('toggle_field=yes');
// No selected
$pages->find('toggle_field=0');
$pages->find('toggle_field=no');
// No selection (unknown)
$pages->find('toggle_field=""');
$pages->find('toggle_field=unknown');
// Yes or no (any selection made)
$pages->find('toggle_field=1|0');
// No or no-selection
$pages->find('toggle_field=0|""');Note:
0(no) and''(unknown/no-selection) are distinct values.toggle_field=0will not match pages with no selection, and vice versa.
Depends on the formatType field setting (configured under the Details tab):
| formatType | Output |
|---|---|
0 Integer (default) | 1, 0, 2, or '' |
1 Boolean | true / false (no-selection stays '') |
2 String | Label text (e.g. "Yes", "No") |
3 Entities | Label text, HTML-entity encoded |
// formatType=1 (Boolean), output formatting ON:
if($page->toggle_field === true) echo "Yes";
if($page->toggle_field === false) echo "No";
if($page->toggle_field === '') echo "No selection";
// formatType=2 (String), output formatting ON:
echo $page->toggle_field; // outputs "Yes", "No", or custom label- Four states:
1=yes,0=no,2=other (optional),''=unknown/no-selection. - With output formatting OFF, always returns int or blank string regardless of formatType.
- Compatible fieldtypes:
Fieldtype,Toggle FieldtypeCheckbox. - Database column:
tinyint NOT NULL, indexed.
Toggle fieldtype for “yes/on”, “no/off” and optional “other” state. Unlike FieldtypeCheckbox, this Fieldtype can differentiate between a selection of “no” and no-selection (aka unknown state), and it can also optionally support a selection for “other” (with custom label).
When using a selector to find pages matching a particular toggle state,
or when setting values to $page->your_field, the following:
0ornoorFieldtypefor no/off selectionToggle::valueNo 1oryesorFieldtypefor yes/on selectionToggle::valueYes 2orotherorFieldtypefor other selection (if enabled for field)Toggle::valueOther ''blank string orunknownorFieldtypefor “no selection”Toggle::valueUnknown
Please note that 0 and “no selection” are different things (unlike with a checkbox) so
be sure to consider this when finding pages or outputting values. The examples below
include a couple that demonstrate this.
Examples (for field named “featured”):
// find pages with “yes” selected for “featured”
$items = $pages->find("featured=1");
$items = $pages->find("featured=yes");
// find pages with “no” selected for “featured”
$items = $pages->find("featured=0");
$items = $pages->find("featured=no");
// find pages with no selection
$items = $pages->find("featured=''");
$items = $pages->find("featured=unknown");
// find pages with yes or no selection
$items = $pages->find("featured=1|0");
$items = $pages->find("featured=yes|no");
// find pages with “no” selected, or no selection
$items = $pages->find("featured=''|0");
$items = $pages->find("featured=unknown|no");
// output current value (blank, 0 or 1, or 2 if “other” option available)
// unless you’ve configured it to output custom labels when formatted
echo $page->featured;
// determine current setting (assuming labels not overriding values)
if($page->featured '') {
// unknown aka no-selection
} else if($page->featured 0) {
// no selected
} else if($page->featured 1) {
// yes selected
} else if($page->featured 2) {
// other selected (if enabled)
}
// set value of $page->featured to yes/on
$page->featured = 1; Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Fieldtype class also inherits all the methods and properties of: Fieldtype, WireData and Wire.
Common
Finding
| Name | Return | Summary | |
|---|---|---|---|
Fieldtype Fieldtype Fieldtype | bool | Return whether the given value is considered empty or not. |
Additional methods and properties
In addition to the methods and properties above, Fieldtype
API reference based on ProcessWire core version 3.0.261