Skip to content

Commit

Permalink
Merge pull request #28812 from colemanw/removeContactTypeField
Browse files Browse the repository at this point in the history
APIv4 - Hide 'contact_type' from Individual,Organization,Household getFields
  • Loading branch information
eileenmcnaughton authored Jan 1, 2024
2 parents d2bc2d6 + f8925ce commit 94e70f3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Civi/Api4/Generic/BasicGetFieldsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ protected function formatResults(&$values, $isInternal) {
}
$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';
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public static function on_civi_search_defaultDisplay(GenericHookEvent $e) {
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'],
Expand All @@ -64,7 +70,7 @@ public static function on_civi_search_defaultDisplay(GenericHookEvent $e) {
'key' => 'sort_name',
'icons' => [
['field' => 'contact_sub_type:icon'],
['field' => 'contact_type:icon'],
$contactTypeIcon,
],
],
[
Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/api/v4/Action/GetExtraFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Civi\Api4\Address;
use Civi\Api4\Contact;
use Civi\Api4\Household;
use Civi\Api4\Individual;
use Civi\Api4\Tag;

/**
Expand Down Expand Up @@ -62,6 +63,16 @@ public function testGetFieldsByContactType(): void {
$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);

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v4/Action/GetFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testContactGetFields(): void {
->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
Expand Down

0 comments on commit 94e70f3

Please sign in to comment.