From 92f06cdd1e7f5678a9be7fc112ba0410302e10ec Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 2 Mar 2020 09:33:59 +1100 Subject: [PATCH] Add in code doucmentation of the format of the fields array and add in a unit test --- CRM/Core/BAO/UFField.php | 1 + CRM/Utils/Hook.php | 1 + tests/phpunit/CRM/Core/BAO/UFFieldTest.php | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CRM/Core/BAO/UFField.php b/CRM/Core/BAO/UFField.php index 84fbeb20a8..aaec9715ec 100644 --- a/CRM/Core/BAO/UFField.php +++ b/CRM/Core/BAO/UFField.php @@ -1031,6 +1031,7 @@ SELECT id unset($fields[$value['field_type']][$key]); } + // Allow extensions to alter the array of entity => fields permissible in a CiviCRM Profile. CRM_Utils_Hook::alterUFFields($fields); return $fields; } diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 982da09c58..3c0da9e257 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -2634,6 +2634,7 @@ abstract class CRM_Utils_Hook { /** * Allow extensions to modify the array of acceptable fields to be included on profiles * @param array $fields + * format is [Entity => array of DAO fields] * @return mixed */ public static function alterUFFields(&$fields) { diff --git a/tests/phpunit/CRM/Core/BAO/UFFieldTest.php b/tests/phpunit/CRM/Core/BAO/UFFieldTest.php index ba369748cd..1cafb95c0e 100644 --- a/tests/phpunit/CRM/Core/BAO/UFFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/UFFieldTest.php @@ -218,4 +218,20 @@ class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { return $ufGroup->id; } + /** + * Test ability to modify the acceptable fields for use in a profile via hook + */ + public function testGetFieldsFlatModifiedByHook() { + unset(Civi::$statics['UFFieldsFlat']); + $this->hookClass->setHook('civicrm_alterUFFields', [$this, 'modifyUFFields']); + $fields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); + + $this->assertEquals('Grant', $fields['grant_id']['field_type']); + $this->assertEquals('contact_id', $fields['grant_contact_id']['name']); + } + + public function modifyUFFields(&$fields) { + $fields['Grant'] = CRM_Grant_DAO_Grant::export(); + } + } -- 2.25.1