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
| Name | Return | Summary | |
|---|---|---|---|
Fieldtype | mixed | Format the given value for output and return a string of the formatted value | |
Fieldtype | string | Get the blank value | |
Fieldtype | Fieldtypes null | Get an array of Fieldtypes that are compatible with this one | |
Fieldtype | InputfieldWrapper | Get any Inputfields used for configuration of this Fieldtype. | |
Fieldtype | array | Get database schema | |
Fieldtype | InputfieldToggle | Get the InputfieldToggle instance | |
Fieldtype | DatabaseQuerySelect PageFinderDatabaseQuerySelect | @param DatabaseQuerySelect|PageFinderDatabaseQuerySelect $query | |
Fieldtype | array | Get information used for InputfieldSelector interactive selector builder | |
Fieldtype | string | Return the markup value | |
Fieldtype | string | Sanitize value for placement on Page object | |
Fieldtype | string | Given an 'awake' value, as set by wakeupValue(), convert the value back to a basic type for storage in database. |
Additional methods and properties
In addition to the methods and properties above, Fieldtype
API reference based on ProcessWire core version 3.0.255