From f8925cef1fa848cf8e753f9917a308e3fcf69ac4 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sat, 30 Dec 2023 09:58:44 -0600 Subject: [PATCH] APIv4 - Remove 'contact_type' from Individual,Organization,Household This field is irrelevant to these pseudo-entities and shouldn't need to be used at all. Before: Creating a new SearchKit search for Individual includes Contact Type in the default columns. After: Now it doesn't, and the field is completely hidden for all but the Contact entity. --- Civi/Api4/Generic/BasicGetFieldsAction.php | 6 ++++++ .../Autocomplete/ContactAutocompleteProvider.php | 8 +++++++- tests/phpunit/api/v4/Action/GetExtraFieldsTest.php | 11 +++++++++++ tests/phpunit/api/v4/Action/GetFieldsTest.php | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index 8decd444ce..0c38929f2b 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -146,6 +146,12 @@ class BasicGetFieldsAction extends BasicGetAction { } $field = array_diff_key($field, $internalProps); } + // Hide the 'contact_type' field from Individual,Organization,Household pseudo-entities + if (!$isInternal && $this->getEntityName() !== 'Contact' && CoreUtil::isContact($this->getEntityName())) { + $values = array_filter($values, function($field) { + return $field['name'] !== 'contact_type'; + }); + } } /** diff --git a/Civi/Api4/Service/Autocomplete/ContactAutocompleteProvider.php b/Civi/Api4/Service/Autocomplete/ContactAutocompleteProvider.php index c031c25821..4a7a14e85b 100644 --- a/Civi/Api4/Service/Autocomplete/ContactAutocompleteProvider.php +++ b/Civi/Api4/Service/Autocomplete/ContactAutocompleteProvider.php @@ -54,6 +54,12 @@ class ContactAutocompleteProvider extends \Civi\Core\Service\AutoService impleme if ($e->display['settings'] || $e->display['type'] !== 'autocomplete' || !CoreUtil::isContact($e->savedSearch['api_entity'])) { return; } + if ($e->savedSearch['api_entity'] === 'Contact') { + $contactTypeIcon = ['field' => 'contact_type:icon']; + } + else { + $contactTypeIcon = ['icon' => CoreUtil::getInfoItem($e->savedSearch['api_entity'], 'icon')]; + } $e->display['settings'] = [ 'sort' => [ ['sort_name', 'ASC'], @@ -64,7 +70,7 @@ class ContactAutocompleteProvider extends \Civi\Core\Service\AutoService impleme 'key' => 'sort_name', 'icons' => [ ['field' => 'contact_sub_type:icon'], - ['field' => 'contact_type:icon'], + $contactTypeIcon, ], ], [ diff --git a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php index ec50d50973..27f56c525c 100644 --- a/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php +++ b/tests/phpunit/api/v4/Action/GetExtraFieldsTest.php @@ -24,6 +24,7 @@ use Civi\Api4\Activity; use Civi\Api4\Address; use Civi\Api4\Contact; use Civi\Api4\Household; +use Civi\Api4\Individual; use Civi\Api4\Tag; /** @@ -62,6 +63,16 @@ class GetExtraFieldsTest extends Api4TestBase { $this->assertArrayHasKey('household_name', $householdFields); } + public function testContactPseudoEntityGetFields(): void { + $individualFields = (array) Individual::getFields(FALSE) + ->execute()->indexBy('name'); + $this->assertArrayNotHasKey('sic_code', $individualFields); + $this->assertArrayNotHasKey('contact_type', $individualFields); + $this->assertArrayHasKey('last_name', $individualFields); + $this->assertEquals('Individual', $individualFields['birth_date']['entity']); + $this->assertEquals('Individual', $individualFields['age_years']['entity']); + } + public function testGetOptionsAddress(): void { $getFields = Address::getFields(FALSE)->addWhere('name', '=', 'state_province_id')->setLoadOptions(TRUE); diff --git a/tests/phpunit/api/v4/Action/GetFieldsTest.php b/tests/phpunit/api/v4/Action/GetFieldsTest.php index 93e769f3c8..8aeab52cc8 100644 --- a/tests/phpunit/api/v4/Action/GetFieldsTest.php +++ b/tests/phpunit/api/v4/Action/GetFieldsTest.php @@ -66,7 +66,7 @@ class GetFieldsTest extends Api4TestBase implements TransactionalInterface { ->execute() ->indexBy('name'); // Ensure table & column are returned - $this->assertEquals('civicrm_contact', $fields['display_name']['table_name']); + $this->assertEquals('civicrm_contact', $fields['contact_type']['table_name']); $this->assertEquals('display_name', $fields['display_name']['column_name']); // Check suffixes -- 2.25.1