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

Add support for <hr> elements in InputfieldSelect/InputfieldSelectMultiple per processwire/processwire-requests#508

This commit is contained in:
Ryan Cramer
2024-02-02 12:50:38 -05:00
parent ddbbbcc4e6
commit 9af0aaf2b2

View File

@@ -8,7 +8,7 @@
* Sublcasses will want to override the render method, but it's not necessary to override processInput(). * Sublcasses will want to override the render method, but it's not necessary to override processInput().
* Subclasses that select multiple values should implement the InputfieldHasArrayValue interface. * Subclasses that select multiple values should implement the InputfieldHasArrayValue interface.
* *
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer * ProcessWire 3.x, Copyright 2024 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @property string|int $defaultValue * @property string|int $defaultValue
@@ -39,6 +39,16 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
*/ */
protected $optionLanguageLabels = array(); protected $optionLanguageLabels = array();
/**
* Option separator values
*
* This is so we can validate whether a value is a separator or intentional dashes/hyphens value.
*
* @var array
*
*/
protected $optionHrs = array();
/** /**
* Return information about this module * Return information about this module
* *
@@ -47,7 +57,7 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
return array( return array(
'title' => __('Select', __FILE__), // Module Title 'title' => __('Select', __FILE__), // Module Title
'summary' => __('Selection of a single value from a select pulldown', __FILE__), // Module Summary 'summary' => __('Selection of a single value from a select pulldown', __FILE__), // Module Summary
'version' => 102, 'version' => 103,
'permanent' => true, 'permanent' => true,
); );
} }
@@ -68,6 +78,8 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
* If you want to add an optgroup, use the $value param as the label, and the label param as an array of options. * If you want to add an optgroup, use the $value param as the label, and the label param as an array of options.
* Note that optgroups may not be applicable to other Inputfields that descend from InputfieldSelect. * Note that optgroups may not be applicable to other Inputfields that descend from InputfieldSelect.
* *
* If you want to add an option separator (rather than an option value) use `addOption('---');` (3.0.236+)
*
* @param string $value Value that the option submits (or label of optgroup, if specifying an optgroup) * @param string $value Value that the option submits (or label of optgroup, if specifying an optgroup)
* @param string $label|array Optional label associated with the value (if null, value will be used as the label), or array of optgroup options [value=>label] * @param string $label|array Optional label associated with the value (if null, value will be used as the label), or array of optgroup options [value=>label]
* @param array $attributes Optional attributes to be associated with this option (i.e. a 'selected' attribute for an <option> tag) * @param array $attributes Optional attributes to be associated with this option (i.e. a 'selected' attribute for an <option> tag)
@@ -76,6 +88,11 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
*/ */
public function addOption($value, $label = null, array $attributes = null) { public function addOption($value, $label = null, array $attributes = null) {
if(is_null($label) || (is_string($label) && !strlen($label))) $label = $value; if(is_null($label) || (is_string($label) && !strlen($label))) $label = $value;
if($value === $label && strpos($value, '-') === 0 && trim($value, '-') === '') {
while(isset($this->options[$value])) $value .= '-';
$this->optionHrs[$value] = $value;
$label = '-';
}
if(isset($this->options[$value])) unset($this->options[$value]); if(isset($this->options[$value])) unset($this->options[$value]);
$this->options[$value] = $label; $this->options[$value] = $label;
if(!is_null($attributes)) $this->optionAttributes[$value] = $attributes; if(!is_null($attributes)) $this->optionAttributes[$value] = $attributes;
@@ -445,6 +462,10 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
if(is_null($options)) $options = $this->options; if(is_null($options)) $options = $this->options;
$is = false; $is = false;
if(strpos("$value", '-') === 0 && isset($this->optionHrs[$value])) {
return false;
}
foreach($options as $key => $option) { foreach($options as $key => $option) {
if(is_array($option)) { if(is_array($option)) {
// fieldgroup // fieldgroup
@@ -642,6 +663,11 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
continue; continue;
} }
if($label === '-' && strpos($value, '-') === 0 && trim($value, '-') === '') {
$out .= '<hr>';
continue;
}
$selected = $this->isOptionSelected($value) ? " selected='selected'" : ''; $selected = $this->isOptionSelected($value) ? " selected='selected'" : '';
$attrs = $this->getOptionAttributes($value); $attrs = $this->getOptionAttributes($value);
unset($attrs['selected'], $attrs['checked'], $attrs['value']); unset($attrs['selected'], $attrs['checked'], $attrs['value']);
@@ -975,6 +1001,7 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
// the following configuration specific to non-Fieldtype use of single/multi-selects // the following configuration specific to non-Fieldtype use of single/multi-selects
$isInputfieldSelect = $this->className() == 'InputfieldSelect'; $isInputfieldSelect = $this->className() == 'InputfieldSelect';
$isInputfieldSelects = $this->className() == 'InputfieldSelect' || $this->className() === 'InputfieldSelectMultiple';
$languages = $this->wire()->languages; $languages = $this->wire()->languages;
/** @var InputfieldTextarea $f */ /** @var InputfieldTextarea $f */
@@ -1009,7 +1036,8 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti
'• ' . $this->_('To keep a separate value and label, separate them with an equals sign. Example: value=My Option') . " \n" . '• ' . $this->_('To keep a separate value and label, separate them with an equals sign. Example: value=My Option') . " \n" .
($isInputfieldSelect ? '• ' . $this->_('To precede your list with a blank option, enter just a equals sign "=" as the first option.') . "\n" : '') . ($isInputfieldSelect ? '• ' . $this->_('To precede your list with a blank option, enter just a equals sign "=" as the first option.') . "\n" : '') .
'• ' . $this->_('To make an option selected, precede it with a plus sign. Example: +My Option') . '• ' . $this->_('To make an option selected, precede it with a plus sign. Example: +My Option') .
($isInputfieldSelect ? "\n" . $this->_('To create an optgroup (option group) indent the options in the group with 3 or more spaces.') : ''); ($isInputfieldSelects ? "\n" . $this->_('To create an optgroup (option group) indent the options in the group with 3 or more spaces.') : '') .
($isInputfieldSelects ? "\n" . $this->_('To add a horizontal rule, specify 3 or more dashes/hyphens on one line with nothing else. Example: ---') : '');
if($languages) $f->notes .= " \n\n**" . $this->_('Multi-language instructions:') . "**\n" . if($languages) $f->notes .= " \n\n**" . $this->_('Multi-language instructions:') . "**\n" .
'• ' . $this->_('We recommend using using `value=label`, where `value` is the same across languages and `label` is translated.') . " \n" . '• ' . $this->_('We recommend using using `value=label`, where `value` is the same across languages and `label` is translated.') . " \n" .
'• ' . $this->_('First define your default language options, and then copy/paste into the other languages and translate labels.') . " \n" . '• ' . $this->_('First define your default language options, and then copy/paste into the other languages and translate labels.') . " \n" .