Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook to alter display value or Custom field value #16921

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

}
46 changes: 46 additions & 0 deletions tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}

}