From 1d6187198f1c9e55b51d05c88147a79927243475 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Mon, 14 Nov 2022 15:34:30 +0100 Subject: [PATCH] fix: [metafields:dropdown] Patched saving multiple fields with custom value --- .../genericElements/Form/Fields/dropdownField.php | 11 ++--------- .../element/genericElements/Form/multiFieldButton.php | 10 +++++++--- .../genericElements/Form/multiFieldScaffold.php | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/templates/element/genericElements/Form/Fields/dropdownField.php b/templates/element/genericElements/Form/Fields/dropdownField.php index b8d81b2..b71b15b 100644 --- a/templates/element/genericElements/Form/Fields/dropdownField.php +++ b/templates/element/genericElements/Form/Fields/dropdownField.php @@ -31,10 +31,6 @@ if (in_array('_custom', array_keys($controlParams['options']))) { } else { $customInputValue = ''; } - $customControlParams = [ - 'value' => $fieldData['value'] ?? null, - 'class' => 'd-none', - ]; $controlParams['class'] .= ' dropdown-custom-value' . "-$seed"; $adaptedField = $fieldData['field'] . '_custom'; $controlParams['templates']['formGroup'] = sprintf( @@ -52,17 +48,14 @@ echo $this->FormFieldMassage->prepareFormElement($this->Form, $controlParams, $f toggleFreetextSelectField($select[0]); $select.attr('onclick', 'toggleFreetextSelectField(this)') $select.parent().find('input.custom-value').attr('oninput', 'updateAssociatedSelect(this)') - // updateAssociatedSelect($select.parent().find('input.custom-value')[0]) - - // Multiple saves in dropdown doesn't work - // But multiple saves for custom works but save the first element as `_custom` + updateAssociatedSelect($select.parent().find('input.custom-value')[0]) }) })() function toggleFreetextSelectField(selectEl) { const $select = $(selectEl) - const show = $select.val() == '_custom' + const show = $select.find('option:selected').hasClass('custom-value') const $container = $(selectEl).parent() let $freetextInput = $container.find('input.custom-value') if (show) { diff --git a/templates/element/genericElements/Form/multiFieldButton.php b/templates/element/genericElements/Form/multiFieldButton.php index 9cfd348..ddc17ca 100644 --- a/templates/element/genericElements/Form/multiFieldButton.php +++ b/templates/element/genericElements/Form/multiFieldButton.php @@ -65,9 +65,13 @@ $seed = 'mfb-' . mt_rand(); brackettedPathStr = explodedPath.map((elem, i) => { return i == 0 ? elem : `[${elem}]` }).join('') - $input.attr('id', dottedPathStr) - .attr('field', dottedPathStr) - .attr('name', brackettedPathStr) + const attrs = ['id', 'field', 'name'] + attrs.forEach((attr) => { + if ($input.attr(attr) !== undefined) { + $input.attr(attr, attr === 'name' ? brackettedPathStr : dottedPathStr) + } + }) + $input .val('') .removeClass('is-invalid') }) diff --git a/templates/element/genericElements/Form/multiFieldScaffold.php b/templates/element/genericElements/Form/multiFieldScaffold.php index 89b405c..3099813 100644 --- a/templates/element/genericElements/Form/multiFieldScaffold.php +++ b/templates/element/genericElements/Form/multiFieldScaffold.php @@ -34,7 +34,7 @@ if (!empty($metaFieldsEntities)) { $fieldData['class'] = 'new-metafield'; } if ($labelPrintedOnce) { // Only the first input can have a label - $fieldData['label'] = false; + $fieldData['label'] = ['text' => '']; } if ($metaTemplateField->formType === 'dropdown') { $fieldData = array_merge_recursive($fieldData, $metaTemplateField->formOptions);