2020-06-04 10:05:45 +02:00
|
|
|
<?php
|
2022-11-14 09:04:35 +01:00
|
|
|
$seed = 's-' . mt_rand();
|
|
|
|
$controlParams = [
|
|
|
|
'type' => 'select',
|
2023-02-23 12:55:18 +01:00
|
|
|
'options' => $fieldData['options'] ?? [],
|
2022-11-14 09:04:35 +01:00
|
|
|
'empty' => $fieldData['empty'] ?? false,
|
|
|
|
'value' => $fieldData['value'] ?? null,
|
|
|
|
'multiple' => $fieldData['multiple'] ?? false,
|
|
|
|
'disabled' => $fieldData['disabled'] ?? false,
|
|
|
|
'class' => ($fieldData['class'] ?? '') . ' formDropdown form-select',
|
|
|
|
'default' => $fieldData['default'] ?? '',
|
|
|
|
];
|
|
|
|
if (!empty($fieldData['field'])) { // used for multi meta-field form
|
|
|
|
$controlParams['field'] = $fieldData['field'];
|
|
|
|
}
|
|
|
|
if (!empty($fieldData['label'])) {
|
|
|
|
$controlParams['label'] = $fieldData['label'];
|
|
|
|
}
|
|
|
|
if ($controlParams['options'] instanceof \Cake\ORM\Query) {
|
|
|
|
$controlParams['options'] = $controlParams['options']->all()->toList();
|
|
|
|
}
|
2023-03-13 10:02:33 +01:00
|
|
|
$initSelect2 = false;
|
|
|
|
if (isset($fieldData['select2']) && $fieldData['select2'] == true) {
|
|
|
|
$initSelect2 = true;
|
2023-02-23 12:55:18 +01:00
|
|
|
$fieldData['select2'] = $fieldData['select2'] === true ? [] : $fieldData['select2'];
|
2023-01-17 09:19:40 +01:00
|
|
|
$controlParams['class'] .= ' select2-input';
|
|
|
|
}
|
2023-02-23 12:55:18 +01:00
|
|
|
$controlParams['class'] .= ' dropdown-custom-value' . "-$seed";
|
2022-11-14 09:04:35 +01:00
|
|
|
if (in_array('_custom', array_keys($controlParams['options']))) {
|
|
|
|
$customInputValue = $this->Form->getSourceValue($fieldData['field']);
|
|
|
|
if (!in_array($customInputValue, $controlParams['options'])) {
|
|
|
|
$controlParams['options'] = array_map(function ($option) {
|
|
|
|
if (is_array($option) && $option['value'] == '_custom') {
|
|
|
|
$option[] = 'selected';
|
|
|
|
}
|
|
|
|
return $option;
|
|
|
|
}, $controlParams['options']);
|
|
|
|
} else {
|
|
|
|
$customInputValue = '';
|
|
|
|
}
|
|
|
|
$adaptedField = $fieldData['field'] . '_custom';
|
|
|
|
$controlParams['templates']['formGroup'] = sprintf(
|
|
|
|
'<label class="col-sm-2 col-form-label form-label" {{attrs}}>{{label}}</label><div class="col-sm-10 multi-metafield-input-container"><div class="d-flex form-dropdown-with-freetext input-group">{{input}}{{error}}%s</div></div>',
|
|
|
|
sprintf('<input type="text" class="form-control custom-value" field="%s" value="%s">', h($adaptedField), h($customInputValue))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
echo $this->FormFieldMassage->prepareFormElement($this->Form, $controlParams, $fieldData);
|
|
|
|
?>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
$(document).ready(function() {
|
|
|
|
const $select = $('select.dropdown-custom-value-<?= $seed ?>')
|
|
|
|
toggleFreetextSelectField($select[0]);
|
|
|
|
$select.attr('onclick', 'toggleFreetextSelectField(this)')
|
|
|
|
$select.parent().find('input.custom-value').attr('oninput', 'updateAssociatedSelect(this)')
|
2022-11-14 15:34:30 +01:00
|
|
|
updateAssociatedSelect($select.parent().find('input.custom-value')[0])
|
2023-03-13 10:02:33 +01:00
|
|
|
<?php if ($initSelect2) : ?>
|
|
|
|
// let $container = $select.closest('.modal-dialog .modal-body')
|
|
|
|
let $container = []
|
2023-01-17 09:19:40 +01:00
|
|
|
if ($container.length == 0) {
|
|
|
|
$container = $(document.body)
|
|
|
|
}
|
2023-02-23 12:55:18 +01:00
|
|
|
const defaultSelect2Options = {
|
2023-01-17 09:19:40 +01:00
|
|
|
dropdownParent: $container,
|
2023-02-23 12:55:18 +01:00
|
|
|
}
|
|
|
|
const passedSelect2Options = <?= json_encode($fieldData['select2']) ?>;
|
|
|
|
const select2Options = Object.assign({}, passedSelect2Options, defaultSelect2Options)
|
|
|
|
$select.select2(select2Options)
|
2023-01-17 09:19:40 +01:00
|
|
|
<?php endif; ?>
|
2022-11-14 09:04:35 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
})()
|
|
|
|
|
|
|
|
function toggleFreetextSelectField(selectEl) {
|
|
|
|
const $select = $(selectEl)
|
2022-11-14 15:34:30 +01:00
|
|
|
const show = $select.find('option:selected').hasClass('custom-value')
|
2022-11-14 09:04:35 +01:00
|
|
|
const $container = $(selectEl).parent()
|
|
|
|
let $freetextInput = $container.find('input.custom-value')
|
|
|
|
if (show) {
|
|
|
|
$freetextInput.removeClass('d-none')
|
|
|
|
} else {
|
|
|
|
$freetextInput.addClass('d-none')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateAssociatedSelect(input) {
|
|
|
|
const $input = $(input)
|
|
|
|
const $select = $input.parent().find('select')
|
|
|
|
const $customOption = $select.find('option.custom-value')
|
|
|
|
$customOption.val($input.val())
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
form div.form-dropdown-with-freetext input.custom-value {
|
|
|
|
flex-grow: 3;
|
2021-10-25 16:20:36 +02:00
|
|
|
}
|
2022-11-14 09:04:35 +01:00
|
|
|
</style>
|