Skip to content

Commit

Permalink
APIv4 - Remove 'contact_type' from Individual,Organization,Household
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
colemanw committed Dec 30, 2023
1 parent 4dcf88a commit 93aa901
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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
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 93aa901

Please sign in to comment.