diff --git a/wire/modules/Inputfield/InputfieldSelect.module b/wire/modules/Inputfield/InputfieldSelect.module index dc218918..d8d7ad5c 100644 --- a/wire/modules/Inputfield/InputfieldSelect.module +++ b/wire/modules/Inputfield/InputfieldSelect.module @@ -8,7 +8,7 @@ * 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. * - * ProcessWire 3.x, Copyright 2023 by Ryan Cramer + * ProcessWire 3.x, Copyright 2024 by Ryan Cramer * https://processwire.com * * @property string|int $defaultValue @@ -39,6 +39,16 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti */ 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 * @@ -47,7 +57,7 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti return array( 'title' => __('Select', __FILE__), // Module Title 'summary' => __('Selection of a single value from a select pulldown', __FILE__), // Module Summary - 'version' => 102, + 'version' => 103, 'permanent' => true, ); } @@ -67,6 +77,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. * 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 $label|array Optional label associated with the value (if null, value will be used as the label), or array of optgroup options [value=>label] @@ -76,6 +88,11 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti */ public function addOption($value, $label = null, array $attributes = null) { 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]); $this->options[$value] = $label; 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; $is = false; + if(strpos("$value", '-') === 0 && isset($this->optionHrs[$value])) { + return false; + } + foreach($options as $key => $option) { if(is_array($option)) { // fieldgroup @@ -642,6 +663,11 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti continue; } + if($label === '-' && strpos($value, '-') === 0 && trim($value, '-') === '') { + $out .= '