1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 17:24:46 +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: * For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php * /wire/core/Fieldtype.php
* *
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer * ProcessWire 3.x, Copyright 2020 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
*/ */
@@ -24,14 +24,41 @@ class FieldtypeCheckbox extends Fieldtype {
); );
} }
/**
* Get blank value for field
*
* @param Page $page
* @param Field $field
* @return int
*
*/
public function getBlankValue(Page $page, Field $field) { public function getBlankValue(Page $page, Field $field) {
return 0; 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) { public function sanitizeValue(Page $page, Field $field, $value) {
return $value ? 1 : 0; 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 = '') { public function ___markupValue(Page $page, Field $field, $value = null, $property = '') {
if(is_null($value)) $value = $page->get($field->name); if(is_null($value)) $value = $page->get($field->name);
$checked = $value ? " checked='checked'" : ""; $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>"; 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) { public function getInputfield(Page $page, Field $field) {
/** @var InputfieldCheckbox $inputfield */ /** @var InputfieldCheckbox $inputfield */
$inputfield = $this->modules->get('InputfieldCheckbox'); $inputfield = $this->wire()->modules->get('InputfieldCheckbox');
$inputfield->set('checkedValue', 1); $inputfield->set('checkedValue', 1);
$inputfield->set('uncheckedValue', 0); $inputfield->set('uncheckedValue', 0);
$value = $page->get($field->name); $value = $page->get($field->name);
if($value) $inputfield->attr('checked', 'checked'); if($value) $inputfield->attr('checked', 'checked');
$inputfield->class = $this->className(); $inputfield->addClass($this->className());
return $inputfield; return $inputfield;
} }
/**
* Get database schema for field
*
* @param Field $field
* @return array
*
*/
public function getDatabaseSchema(Field $field) { public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field); $schema = parent::getDatabaseSchema($field);
$schema['data'] = "tinyint NOT NULL"; $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 $table
* @param string $subfield * @param string $subfield
* @param string $operator * @param string $operator
* @param mixed $value * @param mixed $value
*
* @return DatabaseQuery|DatabaseQuerySelect * @return DatabaseQuery|DatabaseQuerySelect
* @throws WireException
* *
*/ */
public function getMatchQuery($query, $table, $subfield, $operator, $value) { 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 // allow for matching test_checkbox!=1 since non-checked rows don't appear in database
static $n = 0; static $n = 0;
$_table = $table . '_ckbx' . (++$n); $_table = $table . '_ckbx' . (++$n);
@@ -109,9 +151,9 @@ class FieldtypeCheckbox extends Fieldtype {
if($field) {} if($field) {}
$fieldtypes = $this->wire(new Fieldtypes()); $fieldtypes = $this->wire(new Fieldtypes());
$toggleClass = 'FieldtypeToggle'; $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) { if($fieldtype instanceof FieldtypeCheckbox) {
$fieldtypes->add($fieldtype); $fieldtypes->add($fieldtype);
} else if($allowToggle && wireInstanceOf($fieldtype, $toggleClass)) { } else if($allowToggle && wireInstanceOf($fieldtype, $toggleClass)) {

View File

@@ -3,10 +3,13 @@
/** /**
* An Inputfield for handling a single checkbox * 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'); * Note: if you want a checkbox already checked, you need to add a setAttribute('checked', 'checked');
* *
* @property string $checkedValue * @property string $checkedValue Value when checked (default=1)
* @property string $uncheckedValue * @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 $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 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 * @property bool $checkboxOnly Show only the checkbox without label text next to it? (default=false) @since 3.0.144
@@ -26,7 +29,16 @@ class InputfieldCheckbox extends Inputfield {
); );
} }
/**
* Default checked value
*
*/
const checkedValueDefault = 1; const checkedValueDefault = 1;
/**
* Default unchecked value
*
*/
const uncheckedValueDefault = ''; const uncheckedValueDefault = '';
/** /**
@@ -66,8 +78,7 @@ class InputfieldCheckbox extends Inputfield {
* *
*/ */
public function wired() { public function wired() {
/** @var Languages $languages */ $languages = $this->wire()->languages;
$languages = $this->wire('languages');
if($languages) { if($languages) {
foreach($languages as $language) { foreach($languages as $language) {
if(!$language->isDefault()) $this->set("checkboxLabel$language", ""); if(!$language->isDefault()) $this->set("checkboxLabel$language", "");
@@ -93,8 +104,7 @@ class InputfieldCheckbox extends Inputfield {
*/ */
public function ___render() { public function ___render() {
/** @var User $user */ $user = $this->wire()->user;
$user = $this->wire('user');
// label placed to the right of the checkbox // label placed to the right of the checkbox
$checkboxLabel = ''; $checkboxLabel = '';
@@ -152,15 +162,15 @@ class InputfieldCheckbox extends Inputfield {
* *
*/ */
public function ___renderValue() { public function ___renderValue() {
$value = $this->attr('value'); $value = $this->val();
if($value != self::uncheckedValueDefault && $value != $this->uncheckedValue) { 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 = $value === "1" ? $this->_('Checked') : $value;
$value = "<i class='fa fa-check-square-o'></i> $value"; $value = wireIconMarkup('check-square-o') . " $value";
} else { } else {
$value = $this->wire('sanitizer')->entities($this->uncheckedValue); $value = $this->wire()->sanitizer->entities($this->uncheckedValue);
$value = empty($value) ? $this->_('Not checked') : $value; $value = empty($value) ? $this->_('Not checked') : $value;
$value = "<i class='fa fa-square-o'></i> $value"; $value = wireIconMarkup('square-o') . " $value";
} }
return $value; return $value;
} }
@@ -175,7 +185,7 @@ class InputfieldCheckbox extends Inputfield {
*/ */
public function setAttribute($key, $value) { public function setAttribute($key, $value) {
if($key == 'value' && $value && "$value" !== "$this->uncheckedValue") { if($key === 'value' && $value && "$value" !== "$this->uncheckedValue") {
if("$value" !== (string) self::checkedValueDefault) { if("$value" !== (string) self::checkedValueDefault) {
$this->checkedValue = $value; $this->checkedValue = $value;
$this->checkedValueIsLabel = false; $this->checkedValueIsLabel = false;
@@ -187,8 +197,18 @@ class InputfieldCheckbox extends Inputfield {
return parent::setAttribute($key, $value); return parent::setAttribute($key, $value);
} }
/**
* Set property
*
* @param string $key
* @param mixed $value
* @return InputfieldCheckbox|Inputfield
*
*/
public function set($key, $value) { 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); return parent::set($key, $value);
} }
@@ -282,10 +302,10 @@ class InputfieldCheckbox extends Inputfield {
public function ___getConfigInputfields() { public function ___getConfigInputfields() {
$inputfields = parent::___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->attr('name', 'checkboxLabel');
$f->label = $this->_('Checkbox label'); $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.'); $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); $inputfields->add($f);
// if working with fieldtype, no additional settings are applicable
if($this->hasFieldtype) return $inputfields; if($this->hasFieldtype) return $inputfields;
$f = $this->wire('modules')->get('InputfieldText'); $f = $this->wire()->modules->get('InputfieldText');
$f->attr('name', 'checkedValue'); $f->attr('name', 'checkedValue');
$f->attr('value', $this->checkedValue); $f->attr('value', $this->checkedValue);
$f->label = $this->_('Checked Value'); $f->label = $this->_('Checked Value');
@@ -311,7 +332,7 @@ class InputfieldCheckbox extends Inputfield {
$f->required = true; $f->required = true;
$inputfields->add($f); $inputfields->add($f);
$f = $this->wire('modules')->get('InputfieldText'); $f = $this->wire()->modules->get('InputfieldText');
$f->attr('name', 'uncheckedValue'); $f->attr('name', 'uncheckedValue');
$f->attr('value', "$this->uncheckedValue"); $f->attr('value', "$this->uncheckedValue");
$f->label = $this->_('Unchecked Value'); $f->label = $this->_('Unchecked Value');