From 5a87dad2d0e3e7b4c9baf29983678065a9a385ab Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 19 Oct 2023 15:08:38 -0400 Subject: [PATCH] APIv4 - Add CoreUtil::isType() helper Helper function avoids verbose coding pattern. --- CRM/Core/ManagedEntities.php | 4 ++-- Civi/Api4/Generic/Traits/DAOActionTrait.php | 2 +- .../Generic/Traits/SavedSearchInspectorTrait.php | 2 +- Civi/Api4/Utils/CoreUtil.php | 14 ++++++++++++++ .../Action/SearchDisplay/AbstractRunAction.php | 2 +- tests/phpunit/api/v4/Entity/ConformanceTest.php | 4 ++-- .../phpstorm/Civi/PhpStorm/Api4Generator.php | 4 ++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index f080c45e94..09ea4c76a2 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -153,7 +153,7 @@ class CRM_Core_ManagedEntities { // Fetch default values for fields that are writeable $condition = [['type', '=', 'Field'], ['readonly', 'IS EMPTY'], ['default_value', '!=', 'now']]; // Exclude "weight" as that auto-adjusts - if (in_array('SortableEntity', CoreUtil::getInfoItem($item['entity_type'], 'type'), TRUE)) { + if (CoreUtil::isType($item['entity_type'], 'SortableEntity')) { $weightCol = CoreUtil::getInfoItem($item['entity_type'], 'order_by'); $condition[] = ['name', '!=', $weightCol]; } @@ -302,7 +302,7 @@ class CRM_Core_ManagedEntities { $idField = CoreUtil::getIdFieldName($item['entity_type']); $params['values'][$idField] = $item['entity_id']; // Exclude "weight" as that auto-adjusts - if (in_array('SortableEntity', CoreUtil::getInfoItem($item['entity_type'], 'type'), TRUE)) { + if (CoreUtil::isType($item['entity_type'], 'SortableEntity')) { $weightCol = CoreUtil::getInfoItem($item['entity_type'], 'order_by'); unset($params['values'][$weightCol]); } diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index feabd9bc49..3a7d72c4e8 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -102,7 +102,7 @@ trait DAOActionTrait { protected function writeObjects($items) { $updateWeights = FALSE; // Adjust weights for sortable entities - if (in_array('SortableEntity', CoreUtil::getInfoItem($this->getEntityName(), 'type'))) { + if (CoreUtil::isType($this->getEntityName(), 'SortableEntity')) { $weightField = CoreUtil::getInfoItem($this->getEntityName(), 'order_by'); // Only take action if updating a single record, or if no weights are specified in any record // This avoids messing up a bulk update with multiple recalculations diff --git a/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php b/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php index 92487d91ba..f10c09e818 100644 --- a/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php +++ b/Civi/Api4/Generic/Traits/SavedSearchInspectorTrait.php @@ -148,7 +148,7 @@ trait SavedSearchInspectorTrait { */ private function getQuery() { if (!isset($this->_selectQuery) && !empty($this->savedSearch['api_entity'])) { - if (!in_array('DAOEntity', CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'type'), TRUE)) { + if (!CoreUtil::isType($this->savedSearch['api_entity'], 'DAOEntity')) { return $this->_selectQuery = FALSE; } $api = Request::create($this->savedSearch['api_entity'], 'get', $this->savedSearch['api_params']); diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index 80abe31d5d..b8945e94ca 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -90,6 +90,20 @@ class CoreUtil { return $provider->getEntities()[$entityName][$keyToReturn] ?? NULL; } + /** + * Check if entity is of given type. + * + * @param string $entityName + * e.g. 'Contact' + * @param string $entityType + * e.g. 'SortableEntity' + * @return bool + */ + public static function isType(string $entityName, string $entityType): bool { + $entityTypes = (array) self::getInfoItem($entityName, 'type'); + return in_array($entityType, $entityTypes, TRUE); + } + /** * Get name of unique identifier, typically "id" * @param string $entityName diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index 02037c290a..69acf8e3ff 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -1147,7 +1147,7 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { } // Add primary key field if actions are enabled // (only needed for non-dao entities, as Api4SelectQuery will auto-add the id) - if (!in_array('DAOEntity', CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'type')) && + if (!CoreUtil::isType($this->savedSearch['api_entity'], 'DAOEntity') && (!empty($this->display['settings']['actions']) || !empty($this->display['settings']['draggable'])) ) { $this->addSelectExpression(CoreUtil::getIdFieldName($this->savedSearch['api_entity'])); diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index 7c656b61f6..37cae60615 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -441,7 +441,7 @@ class ConformanceTest extends Api4TestBase implements HookInterface { $deleteResult = $deleteAction->execute(); }); - if (in_array('DAOEntity', CoreUtil::getInfoItem($entityName, 'type'))) { + if (CoreUtil::isType($entityName, 'DAOEntity')) { // We should have emitted an event. $hookEntity = ($entityName === 'Contact') ? 'Individual' : $entityName;/* ooph */ $this->assertContains("pre.{$hookEntity}.delete", $log, "$entityName should emit hook_civicrm_pre() for deletions"); @@ -520,7 +520,7 @@ class ConformanceTest extends Api4TestBase implements HookInterface { * @return bool */ protected function isReadOnly($entityName) { - return in_array('ReadOnlyEntity', CoreUtil::getInfoItem($entityName, 'type'), TRUE); + return CoreUtil::isType($entityName, 'ReadOnlyEntity'); } /** diff --git a/tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php b/tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php index 532d315180..04d2dae43f 100644 --- a/tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php +++ b/tools/extensions/phpstorm/Civi/PhpStorm/Api4Generator.php @@ -34,11 +34,13 @@ class Api4Generator extends AutoService implements EventSubscriberInterface { $entities = Entity::get(FALSE)->addSelect('name')->execute()->column('name'); $actions = ['get', 'save', 'create', 'update', 'delete', 'replace', 'revert', 'export', 'autocomplete', 'getFields', 'getActions', 'checkAccess']; $properties = Entity::getFields(FALSE)->addOrderBy('name')->execute()->column('name'); + $entityTypes = Entity::getFields(FALSE)->addWhere('name', '=', 'type')->setLoadOptions(TRUE)->execute()->first()['options'] ?? []; $builder = new PhpStormMetadata('api4', __CLASS__); $builder->registerArgumentsSet('api4Entities', ...$entities); $builder->registerArgumentsSet('api4Actions', ...$actions); $builder->registerArgumentsSet('api4Properties', ...$properties); + $builder->registerArgumentsSet('api4EntityTypes', ...array_values($entityTypes)); // Define arguments for core functions $builder->addExpectedArguments('\civicrm_api4()', 0, 'api4Entities'); @@ -52,6 +54,8 @@ class Api4Generator extends AutoService implements EventSubscriberInterface { $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::getTableName()', 0, 'api4Entities'); $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::getCustomGroupExtends()', 0, 'api4Entities'); $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::getRefCount()', 0, 'api4Entities'); + $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::isType()', 0, 'api4Entities'); + $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::isType()', 1, 'api4EntityTypes'); $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::checkAccessDelegated()', 0, 'api4Entities'); $builder->addExpectedArguments('\Civi\Api4\Utils\CoreUtil::checkAccessDelegated()', 1, 'api4Actions'); $builder->addExpectedArguments('\Civi\API\EntityLookupTrait::define()', 0, 'api4Entities'); -- 2.25.1