From 0bdc1b6d3bbaf7f68c9275234d1ad5996078d48a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 12 Sep 2020 11:06:08 -0400 Subject: [PATCH] Allow custom fields of type Autocomplete-Select to be multivalued --- CRM/Core/BAO/CustomField.php | 7 ++++++- CRM/Custom/Form/Field.php | 2 +- templates/CRM/Custom/Form/Field.tpl | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 55e1b786bb..afba04879b 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -918,7 +918,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $attributes += [ 'entity' => 'OptionValue', 'placeholder' => $placeholder, - 'multiple' => $search, + 'multiple' => $search ? TRUE : !empty($field->serialize), 'api' => [ 'params' => ['option_group_id' => $field->option_group_id, 'is_active' => 1], ], @@ -1456,6 +1456,11 @@ SELECT id } } elseif (self::isSerialized($customFields[$customFieldId])) { + // Select2 v3 returns a comma-separated string. + if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) { + $value = explode(',', $value); + } + $value = $value ? CRM_Utils_Array::implodePadded($value) : ''; } diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index ba68cc706a..89dfdc93a3 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -844,7 +844,7 @@ AND option_group_id = %2"; $params['is_search_range'] = 0; } - if ($params['html_type'] === 'Select') { + if ($params['data_type'] !== 'ContactReference' && ($params['html_type'] === 'Select' || $params['html_type'] === 'Autocomplete-Select')) { $params['serialize'] = $params['serialize'] ? CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND : 'null'; } else { diff --git a/templates/CRM/Custom/Form/Field.tpl b/templates/CRM/Custom/Form/Field.tpl index aeb56d695b..75c9fed247 100644 --- a/templates/CRM/Custom/Form/Field.tpl +++ b/templates/CRM/Custom/Form/Field.tpl @@ -277,7 +277,7 @@ $("#noteColumns, #noteRows, #noteLength", $form).toggle(dataType === 'Memo'); - $(".crm-custom-field-form-block-serialize", $form).toggle(htmlType === 'Select'); + $(".crm-custom-field-form-block-serialize", $form).toggle((htmlType === 'Select' || htmlType === 'Autocomplete-Select') && dataType !== 'ContactReference'); } function makeDefaultValueField(dataType) { @@ -308,7 +308,7 @@ $form.submit(function() { var htmlType = $('#html_type', $form).val(), serialize = $("#serialize", $form).is(':checked'), - htmlTypeLabel = (serialize && htmlType === 'Select') ? ts('Multi-Select') : _.find(htmlTypes, {key: htmlType}).value; + htmlTypeLabel = (serialize && _.includes(['Select', 'Autocomplete-Select'], htmlType)) ? ts('Multi-Select') : _.find(htmlTypes, {key: htmlType}).value; if (originalHtmlType && (originalHtmlType !== htmlType || originalSerialize !== serialize)) { var origHtmlTypeLabel = (originalSerialize && originalHtmlType === 'Select') ? ts('Multi-Select') : _.find(htmlTypes, {key: originalHtmlType}).value; if (originalSerialize && !serialize && existingMultiValueCount) { -- 2.25.1