1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00

Minor code updates to Checkbox Fieldtype and Inputfield

This commit is contained in:
Ryan Cramer
2020-08-20 10:23:12 -04:00
parent 0dda488739
commit f95705dde4
2 changed files with 94 additions and 31 deletions

View File

@@ -8,7 +8,7 @@
* For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php
*
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
* https://processwire.com
*
*/
@@ -21,17 +21,44 @@ class FieldtypeCheckbox extends Fieldtype {
'version' => 101,
'summary' => 'This Fieldtype stores an ON/OFF toggle via a single checkbox. The ON value is 1 and OFF value is 0.',
'permanent' => true,
);
);
}
/**
* Get blank value for field
*
* @param Page $page
* @param Field $field
* @return int
*
*/
public function getBlankValue(Page $page, Field $field) {
return 0;
}
/**
* Sanitize value for field
*
* @param Page $page
* @param Field $field
* @param mixed $value
* @return int
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
return $value ? 1 : 0;
}
/**
* Get markup value for field
*
* @param Page $page
* @param Field $field
* @param int|null $value
* @param string $property
* @return MarkupFieldtype|string
*
*/
public function ___markupValue(Page $page, Field $field, $value = null, $property = '') {
if(is_null($value)) $value = $page->get($field->name);
$checked = $value ? " checked='checked'" : "";
@@ -40,17 +67,32 @@ class FieldtypeCheckbox extends Fieldtype {
return "<input type='checkbox' $checked disabled='disabled' /><span style='display:none'>$textValue</span>";
}
/**
* Get Inputfield for field
*
* @param Page $page
* @param Field $field
* @return InputfieldCheckbox
*
*/
public function getInputfield(Page $page, Field $field) {
/** @var InputfieldCheckbox $inputfield */
$inputfield = $this->modules->get('InputfieldCheckbox');
$inputfield = $this->wire()->modules->get('InputfieldCheckbox');
$inputfield->set('checkedValue', 1);
$inputfield->set('uncheckedValue', 0);
$value = $page->get($field->name);
if($value) $inputfield->attr('checked', 'checked');
$inputfield->class = $this->className();
$inputfield->addClass($this->className());
return $inputfield;
}
/**
* Get database schema for field
*
* @param Field $field
* @return array
*
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = "tinyint NOT NULL";
@@ -59,18 +101,18 @@ class FieldtypeCheckbox extends Fieldtype {
}
/**
* @param DatabaseQuerySelect $query
* Get match query for PageFinder
*
* @param PageFinderDatabaseQuerySelect|DatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param mixed $value
*
* @return DatabaseQuery|DatabaseQuerySelect
* @throws WireException
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
if(!empty($value) && ($operator == '!=' || $operator == '<>')) {
if(!empty($value) && ($operator === '!=' || $operator === '<>')) {
// allow for matching test_checkbox!=1 since non-checked rows don't appear in database
static $n = 0;
$_table = $table . '_ckbx' . (++$n);
@@ -109,9 +151,9 @@ class FieldtypeCheckbox extends Fieldtype {
if($field) {}
$fieldtypes = $this->wire(new Fieldtypes());
$toggleClass = 'FieldtypeToggle';
$allowToggle = $this->wire('modules')->isInstalled($toggleClass);
$allowToggle = $this->wire()->modules->isInstalled($toggleClass);
foreach($this->wire('fieldtypes') as $fieldtype) {
foreach($this->wire()->fieldtypes as $fieldtype) {
if($fieldtype instanceof FieldtypeCheckbox) {
$fieldtypes->add($fieldtype);
} else if($allowToggle && wireInstanceOf($fieldtype, $toggleClass)) {

View File

@@ -3,10 +3,13 @@
/**
* An Inputfield for handling a single checkbox
*
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer
* https://processwire.com
*
* Note: if you want a checkbox already checked, you need to add a setAttribute('checked', 'checked');
*
* @property string $checkedValue
* @property string $uncheckedValue
* @property string $checkedValue Value when checked (default=1)
* @property string $uncheckedValue Value when not checked (default='', blank string)
* @property string $label2 Alterate label to display next to checkbox (default=use regular label)
* @property string $checkboxLabel Same as label2, but used as part of field config rather than API-only config.
* @property bool $checkboxOnly Show only the checkbox without label text next to it? (default=false) @since 3.0.144
@@ -23,10 +26,19 @@ class InputfieldCheckbox extends Inputfield {
'summary' => __('Single checkbox toggle', __FILE__), // Module Summary
'version' => 106,
'permanent' => true,
);
);
}
/**
* Default checked value
*
*/
const checkedValueDefault = 1;
/**
* Default unchecked value
*
*/
const uncheckedValueDefault = '';
/**
@@ -66,8 +78,7 @@ class InputfieldCheckbox extends Inputfield {
*
*/
public function wired() {
/** @var Languages $languages */
$languages = $this->wire('languages');
$languages = $this->wire()->languages;
if($languages) {
foreach($languages as $language) {
if(!$language->isDefault()) $this->set("checkboxLabel$language", "");
@@ -93,8 +104,7 @@ class InputfieldCheckbox extends Inputfield {
*/
public function ___render() {
/** @var User $user */
$user = $this->wire('user');
$user = $this->wire()->user;
// label placed to the right of the checkbox
$checkboxLabel = '';
@@ -152,15 +162,15 @@ class InputfieldCheckbox extends Inputfield {
*
*/
public function ___renderValue() {
$value = $this->attr('value');
$value = $this->val();
if($value != self::uncheckedValueDefault && $value != $this->uncheckedValue) {
$value = $this->wire('sanitizer')->entities($this->checkedValue);
$value = $this->wire()->sanitizer->entities($this->checkedValue);
$value = $value === "1" ? $this->_('Checked') : $value;
$value = "<i class='fa fa-check-square-o'></i> $value";
$value = wireIconMarkup('check-square-o') . " $value";
} else {
$value = $this->wire('sanitizer')->entities($this->uncheckedValue);
$value = $this->wire()->sanitizer->entities($this->uncheckedValue);
$value = empty($value) ? $this->_('Not checked') : $value;
$value = "<i class='fa fa-square-o'></i> $value";
$value = wireIconMarkup('square-o') . " $value";
}
return $value;
}
@@ -175,7 +185,7 @@ class InputfieldCheckbox extends Inputfield {
*/
public function setAttribute($key, $value) {
if($key == 'value' && $value && "$value" !== "$this->uncheckedValue") {
if($key === 'value' && $value && "$value" !== "$this->uncheckedValue") {
if("$value" !== (string) self::checkedValueDefault) {
$this->checkedValue = $value;
$this->checkedValueIsLabel = false;
@@ -187,8 +197,18 @@ class InputfieldCheckbox extends Inputfield {
return parent::setAttribute($key, $value);
}
/**
* Set property
*
* @param string $key
* @param mixed $value
* @return InputfieldCheckbox|Inputfield
*
*/
public function set($key, $value) {
if($key == 'checkedValue' && $value != self::checkedValueDefault) $this->checkedValueIsLabel = true;
if($key === 'checkedValue' && $value != self::checkedValueDefault) {
$this->checkedValueIsLabel = true;
}
return parent::set($key, $value);
}
@@ -282,10 +302,10 @@ class InputfieldCheckbox extends Inputfield {
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
/** @var Languages $languages */
$languages = $this->wire('languages');
$languages = $this->wire()->languages;
$f = $this->wire('modules')->get('InputfieldText');
/** @var InputfieldText $f */
$f = $this->wire()->modules->get('InputfieldText');
$f->attr('name', 'checkboxLabel');
$f->label = $this->_('Checkbox label');
$f->description = $this->_('If you want to have separate field and checkbox labels, specify the label that will appear next to the checkbox here.');
@@ -300,9 +320,10 @@ class InputfieldCheckbox extends Inputfield {
}
$inputfields->add($f);
// if working with fieldtype, no additional settings are applicable
if($this->hasFieldtype) return $inputfields;
$f = $this->wire('modules')->get('InputfieldText');
$f = $this->wire()->modules->get('InputfieldText');
$f->attr('name', 'checkedValue');
$f->attr('value', $this->checkedValue);
$f->label = $this->_('Checked Value');
@@ -311,7 +332,7 @@ class InputfieldCheckbox extends Inputfield {
$f->required = true;
$inputfields->add($f);
$f = $this->wire('modules')->get('InputfieldText');
$f = $this->wire()->modules->get('InputfieldText');
$f->attr('name', 'uncheckedValue');
$f->attr('value', "$this->uncheckedValue");
$f->label = $this->_('Unchecked Value');