diff --git a/config/vufind/FeedbackForms.yaml b/config/vufind/FeedbackForms.yaml index 47560a0535a169a6b3e6156119e67ad6a77a742c..0118c04a3f5aeae56f2e7f446c651a67c3b93f93 100644 --- a/config/vufind/FeedbackForms.yaml +++ b/config/vufind/FeedbackForms.yaml @@ -55,6 +55,14 @@ # label (string) Group label (translation key) # options (array) List of select values (translation keys) # +# placeholder (string) Placeholder label (translation key). Used to instruct or force +# (when combined with 'required' attribute) the user to make a selection from the +# options-list. Only select elements with 'options' are supported. +# For text, textarea, email and url elements, a placeholder text can be configured +# by adding a HTML-attribute via 'settings', for example: +# settings: +# - [placeholder, Please select...] +# #----------------------------------------------------------------------------------- forms: diff --git a/module/VuFind/src/VuFind/Form/Form.php b/module/VuFind/src/VuFind/Form/Form.php index 71e5a5e2958bb1acf4847ff415c7f3c56ed09c03..318c63c700eaa9825bdee039f02f5f779b64870b 100644 --- a/module/VuFind/src/VuFind/Form/Form.php +++ b/module/VuFind/src/VuFind/Form/Form.php @@ -220,7 +220,8 @@ class Form extends \Zend\Form\Form implements $element = []; $required = ['type', 'name']; - $optional = ['required', 'help','value', 'inputType', 'group']; + $optional + = ['required', 'help','value', 'inputType', 'group', 'placeholder']; foreach (array_merge($required, $optional) as $field ) { if (!isset($el[$field])) { @@ -245,8 +246,29 @@ class Form extends \Zend\Form\Form implements } if (isset($el['options'])) { $options = []; + $isSelect = $elementType === 'select'; + $placeholder = $element['placeholder'] ?? null; + + if ($isSelect && $placeholder) { + // Add placeholder option (without value) for + // select element. + $options[] = [ + 'value' => '', + 'label' => $this->translate($placeholder), + 'attributes' => [ + 'selected' => 'selected', 'disabled' => 'disabled' + ] + ]; + } foreach ($el['options'] as $option) { - $options[$option] = $this->translate($option); + if ($isSelect) { + $options[] = [ + 'value' => $option, + 'label' => $this->translate($option) + ]; + } else { + $options[$option] = $this->translate($option); + } } $element['options'] = $options; } elseif (isset($el['optionGroups'])) {