From dc916644aecef5303cdd02de2d71a4d504ebea8d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 2 Aug 2022 17:12:29 -0400 Subject: [PATCH] APIv4 Autocomplete - Select icon based on metadata --- Civi/Api4/Activity.php | 1 + Civi/Api4/Contact.php | 1 + Civi/Api4/Entity.php | 5 +++++ Civi/Api4/Generic/AbstractEntity.php | 1 + Civi/Api4/Generic/AutocompleteAction.php | 15 ++++++++++----- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Civi/Api4/Activity.php b/Civi/Api4/Activity.php index 2de02dceed..30925b1a3d 100644 --- a/Civi/Api4/Activity.php +++ b/Civi/Api4/Activity.php @@ -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 { diff --git a/Civi/Api4/Contact.php b/Civi/Api4/Contact.php index cd717f128c..4d1691d4d7 100644 --- a/Civi/Api4/Contact.php +++ b/Civi/Api4/Contact.php @@ -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 */ diff --git a/Civi/Api4/Entity.php b/Civi/Api4/Entity.php index a53248cf3a..6b84d73b9c 100644 --- a/Civi/Api4/Entity.php +++ b/Civi/Api4/Entity.php @@ -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', diff --git a/Civi/Api4/Generic/AbstractEntity.php b/Civi/Api4/Generic/AbstractEntity.php index bc99c02a41..083e3359e2 100644 --- a/Civi/Api4/Generic/AbstractEntity.php +++ b/Civi/Api4/Generic/AbstractEntity.php @@ -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); diff --git a/Civi/Api4/Generic/AutocompleteAction.php b/Civi/Api4/Generic/AutocompleteAction.php index 4ebbf7c7bd..1fc9110ad8 100644 --- a/Civi/Api4/Generic/AutocompleteAction.php +++ b/Civi/Api4/Generic/AutocompleteAction.php @@ -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()); -- 2.25.1