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:

  • 0 or no or FieldtypeToggle::valueNo for no/off selection
  • 1 or yes or FieldtypeToggle::valueYes for yes/on selection
  • 2 or other or FieldtypeToggle::valueOther for other selection (if enabled for field)
  • '' blank string or unknown or FieldtypeToggle::valueUnknown for “no selection”

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 FieldtypeToggle class also inherits all the methods and properties of: Fieldtype, WireData and Wire.

Show class?             Show args?        

Common

NameReturnSummary 
FieldtypeToggle::formatValue(Page $page, Field $field, $value)
mixedFormat the given value for output and return a string of the formatted value
FieldtypeToggle::getBlankValue(Page $page, Field $field)
stringGet the blank value 
FieldtypeToggle::getCompatibleFieldtypes(Field $field)
Fieldtypes nullGet an array of Fieldtypes that are compatible with this one
FieldtypeToggle::getConfigInputfields(Field $field)
InputfieldWrapperGet any Inputfields used for configuration of this Fieldtype.
FieldtypeToggle::getDatabaseSchema(Field $field)
arrayGet database schema 
FieldtypeToggle::getInputfield(Page $page, Field $field)
InputfieldToggleGet the InputfieldToggle instance 
FieldtypeToggle::getMatchQuery($query, string $table, string $subfield, string $operator, mixed $value)
DatabaseQuerySelect PageFinderDatabaseQuerySelect@param DatabaseQuerySelect|PageFinderDatabaseQuerySelect $query 
FieldtypeToggle::getModuleInfo()
(nothing) 
FieldtypeToggle::getSelectorInfo(Field $field)
arrayGet information used for InputfieldSelector interactive selector builder
FieldtypeToggle::markupValue(Page $page, Field $field)
stringReturn the markup value
FieldtypeToggle::sanitizeValue(Page $page, Field $field, $value)
stringSanitize value for placement on Page object 
FieldtypeToggle::sleepValue(Page $page, Field $field, $value)
stringGiven 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, FieldtypeToggle also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #550
    In the 550th issue of ProcessWire Weekly we're going to check out the latest core updates, introduce a new third party module called PagefileMetadata, and more. Read on!
    Weekly.pw / 23 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

I just love the easy and intuitive ProcessWire API. ProcessWire rocks!” —Jens Martsch, Web developer