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)
$this

Add a selectable option (custom API usage only, overrides built-in options)

 
InputfieldToggle::formatLabel(string $label)
string

Format label for HTML output (entity encode, etc.)

 
InputfieldToggle::getAllLabels()
array

Get all possible labels for all label types and all languages

 
InputfieldToggle::getConfigAllowContext(Field $field)
array

Return a list of config property names allowed for fieldgroup/template context

InputfieldToggle::getConfigInputfields()
InputfieldWrapper

Configure Inputfield

InputfieldToggle::getInputfield()
mixed

Get the delegated Inputfield that will be used for rendering selectable options

InputfieldToggle::getLabels()
array

Get labels for the given label type

 
InputfieldToggle::getOptions()
array

Get all selectable options as array of [ value => label ]

 
InputfieldToggle::getValueLabel()
string

Get the label for the currently set (or given) value

 
InputfieldToggle::isEmpty()
bool

Is the current value empty? (i.e. no selection)

 
InputfieldToggle::processInput(WireInputData $input)
$this

Process input

InputfieldToggle::render()
string

Render input element(s)

InputfieldToggle::renderReady()
bool

Render ready

 
InputfieldToggle::renderValue()
string

Render value

InputfieldToggle::sanitizeValue($value)
int string

Sanitize the value to be one ofthe constants: valueYes, valueNo, valueOther, valueUnknown

 
InputfieldToggle::setAttribute($key, $value)
Inputfield

Set attribute

 
InputfieldToggle::setOptions(array $options)
$this

Set all options with array of [ value => label ] (custom API usage only, overrides built-in options)

 
InputfieldToggle::wired()
None 

Properties

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

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.253