From: Seamus Lee Date: Thu, 18 Jun 2020 23:39:58 +0000 (+1000) Subject: Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ab1d98dd07ec04f49b67a93e20ec9a6f8f347d9a;hp=d89f217ddeac0990438952eedb9791dfdd1a7f03;p=civicrm-core.git Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value dev/core#1566 Fix display value for money radio custom field --- diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 7c6350553b..cb699ecad3 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1084,6 +1084,20 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { } else { $display = CRM_Utils_Array::value($value, $field['options'], ''); + // For float type (see Number and Money) $value would be decimal like + // 1.00 (because it is stored in db as decimal), while options array + // key would be integer like 1. In this case expression on line above + // would return empty string (default value), despite the fact that + // matching option exists in the array. + // In such cases we could just get intval($value) and fetch matching + // option again, but this would not work if key is float like 5.6. + // So we need to truncate trailing zeros to make it work as expected. + if ($display === '' && strpos($value, '.') !== FALSE) { + // Use round() to truncate trailing zeros, e.g: + // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69. + $value = (string) round($value, 5); + $display = $field['options'][$value] ?? ''; + } } break; diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 103a5c7bdf..42a74c2026 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -187,6 +187,20 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { '' => '', ], ], + [ + 'data_type' => 'Money', + 'html_type' => 'Radio', + 'option_values' => [ + '10' => '10 USD', + '10.1' => '10.1 USD', + '10.99' => '10.99 USD', + ], + 'tests' => [ + '10 USD' => '10.00', + '10.1 USD' => '10.10', + '10.99 USD' => '10.99', + ], + ], ]; foreach ($fieldsToCreate as $num => $field) { $params = $field + ['label' => 'test field ' . $num, 'custom_group_id' => $customGroup['id']];