From 09ea80f65265633dc5af8168cb42025db581261d Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 15 May 2023 14:55:26 -0700 Subject: [PATCH] (REF) ImportMap - Split the "import-map lookup mechansim" from the "default civicrm-core mappings". --- Civi/Esm/ImportMap.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Civi/Esm/ImportMap.php b/Civi/Esm/ImportMap.php index 707b4644dd..b4d851b54e 100644 --- a/Civi/Esm/ImportMap.php +++ b/Civi/Esm/ImportMap.php @@ -2,6 +2,8 @@ namespace Civi\Esm; +use Civi\Core\HookInterface; + /** * ECMAScript Modules (ESMs) allow you to load a JS file based on a physical-path or a * logical-path. Compare: @@ -18,8 +20,7 @@ namespace Civi\Esm; * { "import": {"civicrm/": "https://example.com/sites/all/modules/civicrm"}} * * - * If you are writing Javascript code for an extension, then you may want to define new - * mappings, e.g. + * If your extension includes ESM files, then you may want to add items to the import-map: * * function myext_civicrm_esmImportMap(array &$importMap, array $context): void { * $importMap['imports']['foo/'] = E::url('js/foo/'); @@ -32,7 +33,7 @@ namespace Civi\Esm; * * @service esm.import_map */ -class ImportMap extends \Civi\Core\Service\AutoService { +class ImportMap extends \Civi\Core\Service\AutoService implements HookInterface { /** * Get the list of imports. @@ -61,11 +62,21 @@ class ImportMap extends \Civi\Core\Service\AutoService { $importMap = \Civi::cache('long')->get('import-map'); if ($importMap === NULL) { $importMap = []; - $importMap['imports']['civicrm/'] = \Civi::paths()->getUrl('[civicrm.root]/'); \CRM_Utils_Hook::esmImportMap($importMap, []); \Civi::cache('long')->set('import-map', $importMap); } return $importMap; } + /** + * Register default mappings on behalf of civicrm-core. + * + * @param array $importMap + * @param array $context + * @return void + */ + public function hook_civicrm_esmImportMap(array &$importMap, array $context): void { + $importMap['imports']['civicrm/'] = \Civi::paths()->getUrl('[civicrm.root]/'); + } + } -- 2.25.1