Skip to content

Commit

Permalink
APIv4 - Remove 'contact_type' from Individual,Organization,Household …
Browse files Browse the repository at this point in the history
…pseudo-entities

This field is irrelevant to these 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 29, 2023
1 parent 4dcf88a commit 3aaec5a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CRM/Contact/DAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/Contact.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0470d0df786c3ac33a435555a3d026fb)
* (GenCodeChecksum:db242115b7297ba205c6abac5b8318c6)
*/

/**
Expand Down Expand Up @@ -604,6 +604,7 @@ public static function &fields() {
],
'where' => 'civicrm_contact.contact_type',
'export' => TRUE,
'contactType' => 'Contact',
'table_name' => 'civicrm_contact',
'entity' => 'Contact',
'bao' => 'CRM_Contact_BAO_Contact',
Expand Down
3 changes: 3 additions & 0 deletions Civi/Api4/Service/Spec/SpecGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ private function addDAOFields(string $entityName, string $action, RequestSpec $s
if (isset($DAOField['contactType']) && $spec->getValue('contact_type') && $DAOField['contactType'] !== $spec->getValue('contact_type')) {
continue;
}
elseif (($DAOField['contactType'] ?? NULL) === 'Contact' && $entityName !== 'Contact') {
continue;
}
if (!empty($DAOField['component']) && !\CRM_Core_Component::isEnabled($DAOField['component'])) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/Utils/FormattingUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public static function contactFieldsToRemove($contactType, $prefix): array {
if (!isset(\Civi::$statics[__CLASS__][__FUNCTION__][$contactType])) {
\Civi::$statics[__CLASS__][__FUNCTION__][$contactType] = [];
foreach (\CRM_Contact_DAO_Contact::fields() as $field) {
if (!empty($field['contactType']) && $field['contactType'] != $contactType) {
if (!empty($field['contactType']) && $field['contactType'] !== 'Contact' && $field['contactType'] !== $contactType) {
\Civi::$statics[__CLASS__][__FUNCTION__][$contactType][] = $field['name'];
// Include suffixed variants like prefix_id:label
if (!empty($field['pseudoconstant'])) {
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/api/v4/Action/GetExtraFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public function testGetFieldsByContactType(): void {

$individualFields = (array) $getFields->setValues(['contact_type' => 'Individual'])->execute()->indexBy('name');
$this->assertArrayNotHasKey('sic_code', $individualFields);
$this->assertTrue($individualFields['contact_type']['readonly']);
$this->assertArrayNotHasKey('contact_type', $individualFields);
$this->assertArrayHasKey('first_name', $individualFields);

$orgId = Contact::create(FALSE)->addValue('contact_type', 'Organization')->execute()->first()['id'];
$organizationFields = (array) $getFields->setValues(['id' => $orgId])->execute()->indexBy('name');
$this->assertArrayHasKey('organization_name', $organizationFields);
$this->assertArrayHasKey('sic_code', $organizationFields);
$this->assertTrue($organizationFields['contact_type']['readonly']);
$this->assertArrayNotHasKey('contact_type', $organizationFields);
$this->assertArrayNotHasKey('first_name', $organizationFields);
$this->assertArrayNotHasKey('household_name', $organizationFields);

$hhId = Household::create(FALSE)->execute()->first()['id'];
$householdFields = (array) $getFields->setValues(['id' => $hhId])->execute()->indexBy('name');
$this->assertArrayNotHasKey('sic_code', $householdFields);
$this->assertTrue($householdFields['contact_type']['readonly']);
$this->assertArrayNotHasKey('contact_type', $householdFields);
$this->assertArrayNotHasKey('first_name', $householdFields);
$this->assertArrayHasKey('household_name', $householdFields);
}
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
1 change: 1 addition & 0 deletions xml/schema/Contact/Contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<readonly>true</readonly>
<add>1.1</add>
<change>3.1</change>
<contactType>Contact</contactType>
</field>
<index>
<name>index_contact_type</name>
Expand Down

0 comments on commit 3aaec5a

Please sign in to comment.