Update CustomField form with 'serialize' option
authorColeman Watts <coleman@civicrm.org>
Mon, 6 Apr 2020 01:23:50 +0000 (21:23 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 14 May 2020 00:03:19 +0000 (20:03 -0400)
CRM/Core/BAO/CustomField.php
CRM/Custom/Form/Field.php
templates/CRM/Custom/Form/Field.tpl

index d9a871d8091fa05f144f91c6923328ba40eee726..9166802f084cfe23aac339a33f043c38641655db 100644 (file)
@@ -1891,7 +1891,12 @@ WHERE  id IN ( %1, %2 )
       $params['date_format'] = Civi::settings()->get('dateInputFormat');
     }
 
-    if ($htmlType === 'CheckBox' || $htmlType === 'Multi-Select') {
+    // Checkboxes are always serialized in current schema
+    if ($htmlType == 'CheckBox') {
+      $params['serialize'] = CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND;
+    }
+
+    if (!empty($params['serialize'])) {
       if (isset($params['default_checkbox_option'])) {
         $defaultArray = [];
         foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
index 0258e068161dc5aa16639a029886568ea1a42ccd..e61d23ed803ce27ae2468052c1eb234dfd82add6 100644 (file)
@@ -327,6 +327,8 @@ class CRM_Custom_Form_Field extends CRM_Core_Form {
       'return' => ['title'],
     ];
 
+    $this->add('checkbox', 'serialize', ts('Multi-Select'));
+
     if ($this->_action == CRM_Core_Action::UPDATE) {
       $this->freeze('data_type');
       if (!empty($this->_values['option_group_id'])) {
@@ -948,6 +950,17 @@ AND    option_group_id = %2";
       $params['is_search_range'] = 0;
     }
 
+    // Serialization cannot be changed on update
+    if ($this->_id) {
+      unset($params['serialize']);
+    }
+    elseif (strpos($params['html_type'], 'Select') === 0) {
+      $params['serialize'] = $params['serialize'] ? CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND : 'null';
+    }
+    else {
+      $params['serialize'] = $params['html_type'] == 'CheckBox' ? CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND : 'null';
+    }
+
     $filter = 'null';
     if ($dataTypeKey == 11 && !empty($params['filter_selected'])) {
       if ($params['filter_selected'] == 'Advance' && trim(CRM_Utils_Array::value('filter', $params))) {
index 040dc348cbb97927f1e32e5dcb92d2e9007d95f9..83a8dd57e338510ba22a6483c1dd995becb9dcfd 100644 (file)
         {/if}
       </td>
     </tr>
+    <tr class="crm-custom-field-form-block-serialize">
+      <td class="label">{$form.serialize.label}</td>
+      <td class="html-adjust">{$form.serialize.html}</td>
+    </tr>
     {if $form.in_selector}
       <tr class='crm-custom-field-form-block-in_selector'>
         <td class='label'>{$form.in_selector.label}</td>
       $("#textLength", $form).toggle(dataType === 'String');
 
       $("#noteColumns, #noteRows, #noteLength", $form).toggle(dataType === 'Memo');
+
+      $(".crm-custom-field-form-block-serialize", $form).toggle(htmlType === 'Select' || htmlType === 'Country' || htmlType === 'StateProvince');
     }
 
     $('[name^="data_type"]', $form).change(customOptionHtmlType);