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 = array_combine($selectOptions, $selectOptions);
if (!empty($this->sane_default)) {
// $selectOptions['_custom'] = __('-- custom value --');
$selectOptions[] = ['value' => '_custom', 'text' => __('-- custom value --'), 'class' => 'custom-value'];
}
$selectOptions[''] = __('-- no value --');
$formOptions['options'] = $selectOptions;
// $formOptions['empty'] = [['value' => '', 'text' => __('-- select an options --'), 'hidden disabled selected value']];
}
return $formOptions;
}

View File

@ -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) {

View File

@ -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')
})

View File

@ -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);