1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-17 12:10:45 +02:00

Add option to InputfieldCheckbox to enable it to render without labels (useful for when rendered as in first/last table column or similar situations)

This commit is contained in:
Ryan Cramer
2019-10-31 15:25:08 -04:00
parent 50e916b72f
commit 6208e42cac

View File

@@ -9,6 +9,7 @@
* @property string $uncheckedValue * @property string $uncheckedValue
* @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 array $labelAttrs Optional attributes for <label> element that surrounds checkbox (default=[]) @since 3.0.141 * @property array $labelAttrs Optional attributes for <label> element that surrounds checkbox (default=[]) @since 3.0.141
* @property int $autocheck When set to 1, setting value attribute to non-blank/non-zero automatically triggers checked. * @property int $autocheck When set to 1, setting value attribute to non-blank/non-zero automatically triggers checked.
* *
@@ -20,7 +21,7 @@ class InputfieldCheckbox extends Inputfield {
return array( return array(
'title' => __('Checkbox', __FILE__), // Module Title 'title' => __('Checkbox', __FILE__), // Module Title
'summary' => __('Single checkbox toggle', __FILE__), // Module Summary 'summary' => __('Single checkbox toggle', __FILE__), // Module Summary
'version' => 105, 'version' => 106,
'permanent' => true, 'permanent' => true,
); );
} }
@@ -48,6 +49,7 @@ class InputfieldCheckbox extends Inputfield {
// alternate label for checkbox (both do the same thing but for different config context) // alternate label for checkbox (both do the same thing but for different config context)
$this->set('label2', ''); // typically specified by API $this->set('label2', ''); // typically specified by API
$this->set('checkboxOnly', false); // render checkbox only, without showing label text
$this->set('checkboxLabel', ''); // typically specified by interactive config $this->set('checkboxLabel', ''); // typically specified by interactive config
$this->set('labelAttrs', array()); // Optional attributes for <label> element that surrounds checkbox (3.0.141+) $this->set('labelAttrs', array()); // Optional attributes for <label> element that surrounds checkbox (3.0.141+)
@@ -79,20 +81,23 @@ class InputfieldCheckbox extends Inputfield {
$label = ''; $label = '';
$user = $this->wire('user'); $user = $this->wire('user');
if($user->language) $label = $this->getSetting("checkboxLabel$user->language"); if(!$this->checkboxOnly) {
if(!$label) $label = $this->getSetting("checkboxLabel"); if($user->language) $label = $this->getSetting("checkboxLabel$user->language");
if(!$label && $this->checkedValueIsLabel) $label = $this->checkedValue; if(!$label) $label = $this->getSetting("checkboxLabel");
if(!$label) $label = $this->getSetting('label2'); if(!$label && $this->checkedValueIsLabel) $label = $this->checkedValue;
if(!$label) $label = $this->getSetting('label2');
$label = trim($label);
}
$this->set('skipLabel', $this->description || $label ? Inputfield::skipLabelFor : Inputfield::skipLabelHeader); $this->set('skipLabel', $this->description || $label ? Inputfield::skipLabelFor : Inputfield::skipLabelHeader);
if(!$label) $label = $this->label; if(!$label && !$this->checkboxOnly) $label = $this->label;
// TBA: if($this->uncheckedValue) return $this->renderRadio(); // TBA: if($this->uncheckedValue) return $this->renderRadio();
$attrs = $this->getAttributes(); $attrs = $this->getAttributes();
$attrs['value'] = $this->checkedValue; $attrs['value'] = $this->checkedValue;
if($this->getSetting('entityEncodeLabel') !== false) { if(strlen($label) && $this->getSetting('entityEncodeLabel') !== false) {
$label = $this->entityEncode($label, Inputfield::textFormatBasic); $label = $this->entityEncode($label, Inputfield::textFormatBasic);
} }
@@ -103,9 +108,9 @@ class InputfieldCheckbox extends Inputfield {
$labelAttrs = ''; $labelAttrs = '';
} }
$out = $out = "<label$labelAttrs><input type='checkbox' " . $this->getAttributesString($attrs) . " />";
"<label$labelAttrs><input type='checkbox' " . $this->getAttributesString($attrs) . " />" . if(strlen($label)) $out .= "<span class='pw-no-select'>$label</span>";
"<span class='pw-no-select'>$label</span></label>"; $out .= "</label>";
return $out; return $out;
} }