From 9946a777bea22001e370cf23b4f5f93499e0f10a Mon Sep 17 00:00:00 2001 From: colemanw Date: Tue, 29 Aug 2023 12:53:04 -0400 Subject: [PATCH] SearchKit - Expose sql functions provided by extensions Before: When an extension provides a SqlFunction class, it works in the api but is not discoverable in the UI After: Discoverable in both SearchKit and the APIv4 Explorer --- Civi/Api4/Utils/CoreUtil.php | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/Civi/Api4/Utils/CoreUtil.php b/Civi/Api4/Utils/CoreUtil.php index 76db7022f7..473c6ff803 100644 --- a/Civi/Api4/Utils/CoreUtil.php +++ b/Civi/Api4/Utils/CoreUtil.php @@ -375,20 +375,28 @@ class CoreUtil { */ public static function getSqlFunctions(): array { $fns = []; - foreach (glob(\Civi::paths()->getPath('[civicrm.root]/Civi/Api4/Query/SqlFunction*.php')) as $file) { - $matches = []; - if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) { - $className = '\Civi\Api4\Query\\' . $matches[1]; - if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) { - $fns[] = [ - 'name' => $className::getName(), - 'title' => $className::getTitle(), - 'description' => $className::getDescription(), - 'params' => $className::getParams(), - 'category' => $className::getCategory(), - 'dataType' => $className::getDataType(), - 'options' => CoreUtil::formatOptionList($className::getOptions(), ['id', 'name', 'label']), - ]; + $path = 'Civi/Api4/Query/SqlFunction*.php'; + // Search CiviCRM core + all active extensions + $directories = [\Civi::paths()->getPath("[civicrm.root]/$path")]; + foreach (\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles() as $ext) { + $directories[] = \CRM_Utils_File::addTrailingSlash(dirname($ext['filePath'])) . $path; + } + foreach ($directories as $directory) { + foreach (glob($directory) as $file) { + $matches = []; + if (preg_match('/(SqlFunction[A-Z_]+)\.php$/', $file, $matches)) { + $className = '\Civi\Api4\Query\\' . $matches[1]; + if (is_subclass_of($className, '\Civi\Api4\Query\SqlFunction')) { + $fns[] = [ + 'name' => $className::getName(), + 'title' => $className::getTitle(), + 'description' => $className::getDescription(), + 'params' => $className::getParams(), + 'category' => $className::getCategory(), + 'dataType' => $className::getDataType(), + 'options' => CoreUtil::formatOptionList($className::getOptions(), ['id', 'name', 'label']), + ]; + } } } } -- 2.25.1