fix: [metafields:dropdown] Patched saving multiple fields with custom value

pull/121/head
Sami Mokaddem 2022-11-14 15:34:30 +01:00
parent b53f2681b4
commit b6fdf37d54
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 10 additions and 15 deletions

View File

@ -25,12 +25,10 @@ class MetaTemplateField extends AppModel
$selectOptions = !empty($this->sane_default) ? $this->sane_default : $this->values_list; $selectOptions = !empty($this->sane_default) ? $this->sane_default : $this->values_list;
$selectOptions = array_combine($selectOptions, $selectOptions); $selectOptions = array_combine($selectOptions, $selectOptions);
if (!empty($this->sane_default)) { if (!empty($this->sane_default)) {
// $selectOptions['_custom'] = __('-- custom value --');
$selectOptions[] = ['value' => '_custom', 'text' => __('-- custom value --'), 'class' => 'custom-value']; $selectOptions[] = ['value' => '_custom', 'text' => __('-- custom value --'), 'class' => 'custom-value'];
} }
$selectOptions[''] = __('-- no value --'); $selectOptions[''] = __('-- no value --');
$formOptions['options'] = $selectOptions; $formOptions['options'] = $selectOptions;
// $formOptions['empty'] = [['value' => '', 'text' => __('-- select an options --'), 'hidden disabled selected value']];
} }
return $formOptions; return $formOptions;
} }

View File

@ -31,10 +31,6 @@ if (in_array('_custom', array_keys($controlParams['options']))) {
} else { } else {
$customInputValue = ''; $customInputValue = '';
} }
$customControlParams = [
'value' => $fieldData['value'] ?? null,
'class' => 'd-none',
];
$controlParams['class'] .= ' dropdown-custom-value' . "-$seed"; $controlParams['class'] .= ' dropdown-custom-value' . "-$seed";
$adaptedField = $fieldData['field'] . '_custom'; $adaptedField = $fieldData['field'] . '_custom';
$controlParams['templates']['formGroup'] = sprintf( $controlParams['templates']['formGroup'] = sprintf(
@ -52,17 +48,14 @@ echo $this->FormFieldMassage->prepareFormElement($this->Form, $controlParams, $f
toggleFreetextSelectField($select[0]); toggleFreetextSelectField($select[0]);
$select.attr('onclick', 'toggleFreetextSelectField(this)') $select.attr('onclick', 'toggleFreetextSelectField(this)')
$select.parent().find('input.custom-value').attr('oninput', 'updateAssociatedSelect(this)') $select.parent().find('input.custom-value').attr('oninput', 'updateAssociatedSelect(this)')
// updateAssociatedSelect($select.parent().find('input.custom-value')[0]) 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`
}) })
})() })()
function toggleFreetextSelectField(selectEl) { function toggleFreetextSelectField(selectEl) {
const $select = $(selectEl) const $select = $(selectEl)
const show = $select.val() == '_custom' const show = $select.find('option:selected').hasClass('custom-value')
const $container = $(selectEl).parent() const $container = $(selectEl).parent()
let $freetextInput = $container.find('input.custom-value') let $freetextInput = $container.find('input.custom-value')
if (show) { if (show) {

View File

@ -65,9 +65,13 @@ $seed = 'mfb-' . mt_rand();
brackettedPathStr = explodedPath.map((elem, i) => { brackettedPathStr = explodedPath.map((elem, i) => {
return i == 0 ? elem : `[${elem}]` return i == 0 ? elem : `[${elem}]`
}).join('') }).join('')
$input.attr('id', dottedPathStr) const attrs = ['id', 'field', 'name']
.attr('field', dottedPathStr) attrs.forEach((attr) => {
.attr('name', brackettedPathStr) if ($input.attr(attr) !== undefined) {
$input.attr(attr, attr === 'name' ? brackettedPathStr : dottedPathStr)
}
})
$input
.val('') .val('')
.removeClass('is-invalid') .removeClass('is-invalid')
}) })

View File

@ -34,7 +34,7 @@ if (!empty($metaFieldsEntities)) {
$fieldData['class'] = 'new-metafield'; $fieldData['class'] = 'new-metafield';
} }
if ($labelPrintedOnce) { // Only the first input can have a label if ($labelPrintedOnce) { // Only the first input can have a label
$fieldData['label'] = false; $fieldData['label'] = ['text' => ''];
} }
if ($metaTemplateField->formType === 'dropdown') { if ($metaTemplateField->formType === 'dropdown') {
$fieldData = array_merge_recursive($fieldData, $metaTemplateField->formOptions); $fieldData = array_merge_recursive($fieldData, $metaTemplateField->formOptions);