FieldtypeToggle class
Configurable yes/no, on/off toggle alternative to a checkbox, plus optional “other” option.
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.252