Skip to content

Commit

Permalink
Merge pull request #16921 from pradpnayak/newHook
Browse files Browse the repository at this point in the history
Add hook to alter display value or Custom field value
  • Loading branch information
yashodha authored Apr 1, 2020
2 parents f3f0ee4 + 3566563 commit a569ecd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
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';
}
}

}

0 comments on commit a569ecd

Please sign in to comment.