From 9e39fec0df7d72e94e3dbaf57681755639f558f4 Mon Sep 17 00:00:00 2001 From: Adam Wood <72983627+webmaster-cses-org-uk@users.noreply.github.com> Date: Wed, 3 Nov 2021 21:04:23 +0000 Subject: [PATCH 1/3] Resolve error on empty multiple-contact field This change removes a potential "getFieldValue failed" error that can be thrown if an empty scalar value occurs in a multiple-contact reference custom field. See https://lab.civicrm.org/dev/core/-/issues/2939 --- CRM/Core/BAO/CustomField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 2ca7070c76cb..5f6dbee42865 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1069,7 +1069,7 @@ private static function formatDisplayValue($value, $field, $entityId = NULL) { case 'Autocomplete-Select': case 'Radio': case 'CheckBox': - if ($field['data_type'] == 'ContactReference' && (is_array($value) || is_numeric($value))) { + if ($field['data_type'] == 'ContactReference' && (is_array($value) || is_numeric($value)) && !empty($value)) { $displayNames = []; foreach ((array) $value as $contactId) { $displayNames[] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name'); From 66102b8ca4bdd520504c2ab1578e6505c73bf174 Mon Sep 17 00:00:00 2001 From: Adam Wood <72983627+webmaster-cses-org-uk@users.noreply.github.com> Date: Wed, 3 Nov 2021 21:27:13 +0000 Subject: [PATCH 2/3] Improve previous fix to avoid returning non-string Further to previous commit, update to avoid returning non-string from method while still protecting against empty input values for multi-valued contact field. --- CRM/Core/BAO/CustomField.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 5f6dbee42865..e1f333295d8c 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1069,12 +1069,19 @@ private static function formatDisplayValue($value, $field, $entityId = NULL) { case 'Autocomplete-Select': case 'Radio': case 'CheckBox': - if ($field['data_type'] == 'ContactReference' && (is_array($value) || is_numeric($value)) && !empty($value)) { - $displayNames = []; - foreach ((array) $value as $contactId) { - $displayNames[] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name'); + if ($field['data_type'] == 'ContactReference' && (is_array($value) || is_numeric($value))) { + if (empty($value)) + { + $display = ''; + } + else + { + $displayNames = []; + foreach ((array) $value as $contactId) { + $displayNames[] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name'); + } + $display = implode(', ', $displayNames); } - $display = implode(', ', $displayNames); } elseif ($field['data_type'] == 'ContactReference') { $display = $value; From 627d36b7c218e7bd7447a2dca8e84b96491dd55e Mon Sep 17 00:00:00 2001 From: Adam Wood <72983627+webmaster-cses-org-uk@users.noreply.github.com> Date: Wed, 3 Nov 2021 21:36:53 +0000 Subject: [PATCH 3/3] Updated proposed addition to house coding style --- CRM/Core/BAO/CustomField.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index e1f333295d8c..644f16fcffe6 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1070,12 +1070,10 @@ private static function formatDisplayValue($value, $field, $entityId = NULL) { case 'Radio': case 'CheckBox': if ($field['data_type'] == 'ContactReference' && (is_array($value) || is_numeric($value))) { - if (empty($value)) - { + if (empty($value)) { $display = ''; } - else - { + else { $displayNames = []; foreach ((array) $value as $contactId) { $displayNames[] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name');