InputfieldToggle class

A toggle providing similar input capability to a checkbox but much more configurable.

An Inputfield for handling an on/off toggle that maintains a boolean value (or null when no selection). This provides an alternative to a single checkbox field.

API usage example

// Default behavior displays toggle of "Yes" and "No":
$f = $modules->get('InputfieldToggle');
$f->attr('name', 'test_toggle_field');
$f->label = 'Do you like toggle fields?';

// Optionally make it show "On" and "Off" (rather than "Yes" and "No"):
$f->labelType = InputfieldToggle::labelTypeOn;

// Optionally set custom labels:
$f->labelType = InputfieldToggle::labelTypeCustom;
$f->yesLabel = 'Yes please';
$f->noLabel = 'No thanks';

// Optionally add an "other" option with label "Not sure":
$f->useOther = true;
$f->otherLabel = 'Not sure';

// Set the value: 0=No, 1=Yes, 2=Other, or blank string '' for no selection (Unknown)
$f->val(1);

// Optionally set to use radio buttons rather than toggle buttons:
$f->inputfieldClass = 'InputfieldRadios';

// Remember to add to your InputfieldForm, InputfieldWrapper or InputfieldFieldset:
$form->add($f);
echo $form->render();

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the InputfieldToggle class also inherits all the methods and properties of: Inputfield, WireData and Wire.

Show class?     Show args?       Only hookable?    

Common

NameReturnSummary 
InputfieldToggle::addOption($value)
$thisAdd a selectable option (custom API usage only, overrides built-in options) 
InputfieldToggle::formatLabel(string $label)
stringFormat label for HTML output (entity encode, etc.) 
InputfieldToggle::getAllLabels()
arrayGet all possible labels for all label types and all languages 
InputfieldToggle::getConfigAllowContext(Field $field)
arrayReturn a list of config property names allowed for fieldgroup/template context
InputfieldToggle::getConfigInputfields()
InputfieldWrapperConfigure Inputfield
InputfieldToggle::getInputfield()
mixedGet the delegated Inputfield that will be used for rendering selectable options
InputfieldToggle::getLabels()
arrayGet labels for the given label type 
InputfieldToggle::getOptions()
arrayGet all selectable options as array of [ value => label ] 
InputfieldToggle::getValueLabel()
stringGet the label for the currently set (or given) value 
InputfieldToggle::isEmpty()
boolIs the current value empty? (i.e. no selection) 
InputfieldToggle::processInput(WireInputData $input)
$thisProcess input
InputfieldToggle::render()
stringRender input element(s)
InputfieldToggle::renderReady()
boolRender ready 
InputfieldToggle::renderValue()
stringRender value
InputfieldToggle::sanitizeValue($value)
int stringSanitize the value to be one ofthe constants: valueYes, valueNo, valueOther, valueUnknown 
InputfieldToggle::setAttribute($key, $value)
InputfieldSet attribute 
InputfieldToggle::setOptions(array $options)
$thisSet all options with array of [ value => label ] (custom API usage only, overrides built-in options) 
InputfieldToggle::wired()
None 

Properties

NameReturnSummaryDefault
InputfieldToggle::defaultOption string Default selected value of 'no', 'yes', 'other' or 'none' none
InputfieldToggle::inputfieldClass string Inputfield class to use or blank for this toggle buttons blank
InputfieldToggle::labelType int Label type to use: 0=yes/no, 1=true/false, 2=on/off, 3=enabled/disabled, 100=custom 0
InputfieldToggle::noLabel string Custom no/off label
InputfieldToggle::otherLabel string Custom label for optional other value Label to use for "other" option ?
InputfieldToggle::useDeselect bool int Allow radios or toggles to be de-selected, enabling possibility of no-selection? false
InputfieldToggle::useOther int bool Use the "other" option? false
InputfieldToggle::useReverse int bool Reverse the order of the Yes/No options? false
InputfieldToggle::useVertical bool int Use vertically oriented radio buttons? Applies only if $inputfieldClass is 'InputfieldRadios' false
InputfieldToggle::value int string Integer value when selection is made or blank string when no selection (0=No, 1=Yes, 2=Other, ''=Unknown) 
InputfieldToggle::yesLabel string Custom yes/on label

Additional methods and properties

In addition to the methods and properties above, InputfieldToggle also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.251