Skip to content

Commit

Permalink
serialize is for custom_field, is_multiple is for custom_group
Browse files Browse the repository at this point in the history
  • Loading branch information
demeritcowboy committed Aug 21, 2021
1 parent 42dffbc commit b99d713
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,9 @@ public static function formatCustomField(
'table_name' => $tableName,
'column_name' => $columnName,
'file_id' => $fileID,
// is_multiple refers to the custom group, serialize refers to the field.
'is_multiple' => $customFields[$customFieldId]['is_multiple'],
'serialize' => $customFields[$customFieldId]['serialize'],
];

//we need to sort so that custom fields are created in the order of entry
Expand Down
11 changes: 8 additions & 3 deletions CRM/Core/BAO/CustomValueTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static function create($customParams, $parentOperation = NULL) {
// would be 'String' for a concatenated set of integers.
// However, the god-forsaken timestamp hack also needs to be kept
// if value is NULL.
$params[$count] = [$value, ($value && $field['is_multiple']) ? 'String' : $type];
$params[$count] = [$value, ($value && $field['serialize']) ? 'String' : $type];
$count++;
}

Expand Down Expand Up @@ -360,7 +360,10 @@ public static function store($params, $entityTable, $entityID, $parentOperation
'custom_group_id' => $customValue['custom_group_id'],
'table_name' => $customValue['table_name'],
'column_name' => $customValue['column_name'],
'is_multiple' => $customValue['is_multiple'] ?? NULL,
// is_multiple refers to the custom group, serialize refers to the field.
// @todo is_multiple can be null - does that mean anything different from 0?
'is_multiple' => $customValue['is_multiple'] ?? CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customValue['custom_group_id'], 'is_multiple'),
'serialize' => $customValue['serialize'] ?? (int) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customValue['custom_field_id'], 'serialize'),
'file_id' => $customValue['file_id'],
];

Expand Down Expand Up @@ -590,7 +593,8 @@ public static function setValues(&$params) {
cf.column_name as column_name,
cf.id as cf_id ,
cf.html_type as html_type ,
cf.data_type as data_type
cf.data_type as data_type ,
cf.serialize as serialize
FROM civicrm_custom_group cg,
civicrm_custom_field cf
WHERE cf.custom_group_id = cg.id
Expand Down Expand Up @@ -648,6 +652,7 @@ public static function setValues(&$params) {
'table_name' => $dao->table_name,
'column_name' => $dao->column_name,
'is_multiple' => $dao->is_multiple,
'serialize' => $dao->serialize,
'extends' => $dao->extends,
];

Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/CRM/Core/BAO/CustomValueTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,52 @@ public function testStoreRichTextEditor() {
$this->contactDelete($contactID);
}

/**
* Test store function for multiselect int.
*/
public function testStoreMultiSelectInt() {
$contactID = $this->individualCreate();
$customGroup = $this->customGroupCreate();
$fields = [
'custom_group_id' => $customGroup['id'],
'data_type' => 'Int',
'html_type' => 'Multi-Select',
'option_values' => [
1 => 'choice1',
2 => 'choice2',
],
'default_value' => '',
];

$customField = $this->customFieldCreate($fields);

$params = [
[
$customField['id'] => [
'value' => CRM_Core_DAO::VALUE_SEPARATOR . '1' . CRM_Core_DAO::VALUE_SEPARATOR . '2' . CRM_Core_DAO::VALUE_SEPARATOR,
'type' => 'Int',
'custom_field_id' => $customField['id'],
'custom_group_id' => $customGroup['id'],
'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
'column_name' => $customField['values'][$customField['id']]['column_name'],
'file_id' => '',
],
],
];

CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);

$customData = \Civi\Api4\Contact::get(FALSE)
->addSelect('new_custom_group.Custom_Field')
->addWhere('id', '=', $contactID)
->execute()->first();
$this->assertEquals([1, 2], $customData['new_custom_group.Custom_Field']);

$this->customFieldDelete($customField['id']);
$this->customGroupDelete($customGroup['id']);
$this->contactDelete($contactID);
}

/**
* Test getEntityValues function for stored value.
*/
Expand Down

0 comments on commit b99d713

Please sign in to comment.