APIv4 - Remove 'contact_type' from Individual,Organization,Household
authorcolemanw <coleman@civicrm.org>
Sat, 30 Dec 2023 15:58:44 +0000 (09:58 -0600)
committercolemanw <coleman@civicrm.org>
Sat, 30 Dec 2023 16:58:48 +0000 (11:58 -0500)
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
Civi/Api4/Service/Autocomplete/ContactAutocompleteProvider.php
tests/phpunit/api/v4/Action/GetExtraFieldsTest.php
tests/phpunit/api/v4/Action/GetFieldsTest.php

index 8decd444ceb87d05b5c7b2e3f303599984daae18..0c38929f2b8138316d0d5aaabbd500b2e015d77c 100644 (file)
@@ -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';
+      });
+    }
   }
 
   /**
index c031c25821c31c836af7db4b327d80ed9481c5ba..4a7a14e85be5febc4fb59375f0201eb65a312a2f 100644 (file)
@@ -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,
           ],
         ],
         [
index ec50d50973269c40a97f702525d812ae409adf26..27f56c525c4c6a02bf8324d43ffad40ceffc9327 100644 (file)
@@ -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);
 
index 93e769f3c8d1d2008a06ca33f8559581d3998798..8aeab52cc85f2853824ed20eb3b8082679181f92 100644 (file)
@@ -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