APIv4 Autocomplete - Select icon based on metadata
authorColeman Watts <coleman@civicrm.org>
Tue, 2 Aug 2022 21:12:29 +0000 (17:12 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 10 Aug 2022 02:28:44 +0000 (22:28 -0400)
Civi/Api4/Activity.php
Civi/Api4/Contact.php
Civi/Api4/Entity.php
Civi/Api4/Generic/AbstractEntity.php
Civi/Api4/Generic/AutocompleteAction.php

index 2de02dceed474a05f6b0008e5be1568658e46089..30925b1a3df3922c2aa0d1432fb02b7f7ee95cd3 100644 (file)
@@ -24,6 +24,7 @@ namespace Civi\Api4;
  * @see https://docs.civicrm.org/user/en/latest/organising-your-data/activities/
  * @searchable primary
  * @since 5.19
+ * @iconField activity_type_id:icon
  * @package Civi\Api4
  */
 class Activity extends Generic\DAOEntity {
index cd717f128c3544886168ff202494ef1a865c1973..4d1691d4d7904e2edc84129e7c594b3b8dfc07be 100644 (file)
@@ -21,6 +21,7 @@ namespace Civi\Api4;
  * @see https://docs.civicrm.org/user/en/latest/organising-your-data/contacts/
  * @searchable primary
  * @orderBy sort_name
+ * @iconField contact_sub_type:icon,contact_type:icon
  * @since 5.19
  * @package Civi\Api4
  */
index a53248cf3ac8c0e752210bb0dba40a46639e1a2f..6b84d73b9ce759ddbf564e68f378c63dba9d8dd1 100644 (file)
@@ -72,6 +72,11 @@ class Entity extends Generic\AbstractEntity {
       'name' => 'label_field',
       'description' => 'Field to show when displaying a record',
     ],
+    [
+      'name' => 'icon_field',
+      'data_type' => 'Array',
+      'description' => 'Field(s) which contain the icon for a record, listed in order of precedence',
+    ],
     [
       'name' => 'order_by',
       'description' => 'Default column to sort results',
index bc99c02a41274641846510f7873e14a25f41c8be..083e3359e2cc4fdda0210ae8ffb6e4a0a658ca9d 100644 (file)
@@ -154,6 +154,7 @@ abstract class AbstractEntity {
       $info['label_field'] = $dao::$_labelField;
       $info['dao'] = $dao;
       $info['table_name'] = $dao::$_tableName;
+      $info['icon_field'] = (array) ($dao::fields()['icon']['name'] ?? NULL);
     }
     foreach (ReflectionUtils::getTraits(static::class) as $trait) {
       $info['type'][] = self::stripNamespace($trait);
index 4ebbf7c7bdddb265ad6fe4549c9874152cf558a5..1fc9110ad83d8a5be77a74562435a1a7f1db77a4 100644 (file)
@@ -65,6 +65,7 @@ class AutocompleteAction extends AbstractAction {
     $fields = CoreUtil::getApiClass($entityName)::get()->entityFields();
     $idField = CoreUtil::getIdFieldName($entityName);
     $labelField = CoreUtil::getInfoItem($entityName, 'label_field');
+    $iconFields = CoreUtil::getInfoItem($entityName, 'icon_field') ?? [];
     $map = [
       'id' => $idField,
       'label' => $labelField,
@@ -76,9 +77,7 @@ class AutocompleteAction extends AbstractAction {
     if (isset($fields['color'])) {
       $map['color'] = 'color';
     }
-    if (isset($fields['icon'])) {
-      $map['icon'] = 'icon';
-    }
+    $select = array_merge(array_values($map), $iconFields);
 
     if (!$this->savedSearch) {
       $this->savedSearch = ['api_entity' => $entityName];
@@ -101,10 +100,10 @@ class AutocompleteAction extends AbstractAction {
       }
     }
     if (empty($this->_apiParams['having'])) {
-      $this->_apiParams['select'] = array_values($map);
+      $this->_apiParams['select'] = $select;
     }
     else {
-      $this->_apiParams['select'] = array_merge($this->_apiParams['select'], array_values($map));
+      $this->_apiParams['select'] = array_merge($this->_apiParams['select'], $select);
     }
     $this->_apiParams['checkPermissions'] = $this->getCheckPermissions();
     $apiResult = civicrm_api4($entityName, 'get', $this->_apiParams);
@@ -114,6 +113,12 @@ class AutocompleteAction extends AbstractAction {
       foreach ($map as $key => $fieldName) {
         $mapped[$key] = $row[$fieldName];
       }
+      foreach ($iconFields as $fieldName) {
+        if (!empty($row[$fieldName])) {
+          $mapped['icon'] = $row[$fieldName];
+          break;
+        }
+      }
       $result[] = $mapped;
     }
     $result->setCountMatched($apiResult->countFetched());