From 21d40964c87507703faa23c3e74fb46d6c0277d2 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Thu, 11 Jul 2019 21:11:38 +0100 Subject: [PATCH 1/2] Add hook to alter display value or Custom field value --- CRM/Core/BAO/CustomField.php | 7 ++++++- CRM/Utils/Hook.php | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index bd50263945e3..05298e5e79c7 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1067,7 +1067,12 @@ public static function displayValue($value, $field, $entityId = NULL) { $fieldInfo = ['options' => $field->getOptions()] + (array) $field; - return self::formatDisplayValue($value, $fieldInfo, $entityId); + $displayValue = self::formatDisplayValue($value, $fieldInfo, $entityId); + + // Call hook to alter display value. + CRM_Utils_Hook::alterCustomFieldDisplayValue($displayValue, $value, $entityId, $fieldInfo); + + return $displayValue; } /** diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 00e217789c80..0d09ab914bfc 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -2649,4 +2649,22 @@ public static function alterUFFields(&$fields) { ); } + /** + * This hook is called to alter Custom field value before its displayed. + * + * @param string $displayValue + * @param mixed $value + * @param int $entityId + * @param array $fieldInfo + * + * @return mixed + */ + public static function alterCustomFieldDisplayValue(&$displayValue, $value, $entityId, $fieldInfo) { + return self::singleton()->invoke( + ['displayValue', 'value', 'entityId', 'fieldInfo'], + $displayValue, $value, $entityId, $fieldInfo, self::$_nullObject, + self::$_nullObject, 'civicrm_alterCustomFieldDisplayValue' + ); + } + } From 3566563f588476fb31794a93745fc24030f399fe Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Sun, 29 Mar 2020 20:57:39 +0100 Subject: [PATCH 2/2] Added unit test --- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 0b97de581618..e0a1cf9b62cc 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -731,4 +731,50 @@ public function testFileDisplayValueNoDescription() { $this->assertEquals($expectedDisplayValue, CRM_Core_BAO_CustomField::displayValue($file['id'], $fileField['id'])); } + /** + * Test for hook_civicrm_alterCustomFieldDisplayValue(). + */ + public function testAlterCustomFieldDisplayValueHook() { + CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_alterCustomFieldDisplayValue', [$this, 'alterCustomFieldDisplayValue']); + $customGroupId = $this->customGroupCreate([ + 'extends' => 'Individual', + 'title' => 'Test Contactcustom Group', + ])['id']; + $fieldId = $this->customFieldCreate([ + 'custom_group_id' => $customGroupId, + 'name' => 'alter_cf_field', + 'label' => 'Alter CF Field', + ])['id']; + $contactId = $this->individualCreate(['custom_' . $fieldId => 'Test']); + + $this->assertEquals('Test', $this->callAPISuccessGetValue('Contact', + ['id' => $contactId, 'return' => "custom_{$fieldId}"] + )); + + $values = []; + $fields = [ + 'custom_' . $fieldId => $this->callAPISuccess('Contact', 'getfield', [ + 'name' => 'custom_' . $fieldId, + 'action' => 'get', + ])['values'], + ]; + + // CRM_Core_BAO_UFGroup::getValues() invokes CRM_Core_BAO_CustomField::displayValue() function. + CRM_Core_BAO_UFGroup::getValues($contactId, $fields, $values); + $this->assertEquals('New value', $values['Alter CF Field']); + } + + /** + * @param string $displayValue + * @param mixed $value + * @param int $entityId + * @param array $fieldInfo + * + */ + public function alterCustomFieldDisplayValue(&$displayValue, $value, $entityId, $fieldInfo) { + if ($fieldInfo['name'] == 'alter_cf_field') { + $displayValue = 'New value'; + } + } + }