From bf9587f579af41c63e1c4a5544f70b35a8304e25 Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Tue, 25 Aug 2020 17:44:52 +0100
Subject: [PATCH] load CRM.payment via coreResourceList so it is added
 everywhere CiviCRM is loaded (eg. drupal_webform etc)

---
 mjwshared.php | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/mjwshared.php b/mjwshared.php
index 9af1596..c9d9ba8 100644
--- a/mjwshared.php
+++ b/mjwshared.php
@@ -27,7 +27,7 @@ function mjwshared_civicrm_config(&$config) {
   // Make sure this runs after everything else but before minifier
   Civi::dispatcher()->addListener('hook_civicrm_buildAsset', 'mjwshared_symfony_civicrm_buildAsset', -990);
   // This should run before (almost) anything else as we're loading shared libraries
-  Civi::dispatcher()->addListener('hook_civicrm_buildForm', 'mjwshared_symfony_civicrm_buildForm', 1000);
+  Civi::dispatcher()->addListener('hook_civicrm_coreResourceList', 'mjwshared_symfony_civicrm_coreResourceList', 1000);
 }
 
 /**
@@ -153,20 +153,21 @@ function mjwshared_civicrm_entityTypes(&$entityTypes) {
 }
 
 /**
- * This hook is invoked when the 'confirm register' and 'thank you' form is rendered
+ * Implements hook_civicrm_coreResourceList().
  */
-function mjwshared_symfony_civicrm_buildForm($event, $hook) {
-  // Load the CRM.payment library
-  // We want this library loaded early. Weights are negative earlier, positive later (opposite to symfony).
-  // CiviCRM "earliest" is -9999 we'll go with -2000 to load after CiviCRM core but before anything else.
-  $region = (CRM_Utils_Request::retrieveValue('is_drupal_webform', 'Boolean', FALSE)) ? 'billing-block' : 'page-header';
-  \Civi::resources()->addScriptUrl(\Civi::service('asset_builder')->getUrl(
-    'crm.payment.js',
-    [
-      'path' => \Civi::resources()->getPath(E::LONG_NAME, 'js/crm.payment.js'),
-      'mimetype' => 'application/javascript',
-    ]
-  ), -2000, $region);
+function mjwshared_symfony_civicrm_coreResourceList($event, $hook) {
+  if ($event->region === 'html-header') {
+    // Load the CRM.payment library
+    // We want this library loaded early. Weights are negative earlier, positive later (opposite to symfony).
+    // CiviCRM "earliest" is -9999 we'll go with -2000 to load after CiviCRM core but before anything else.
+    \Civi::resources()->addScriptUrl(\Civi::service('asset_builder')->getUrl(
+      'crm.payment.js',
+      [
+        'path' => \Civi::resources()->getPath(E::LONG_NAME, 'js/crm.payment.js'),
+        'mimetype' => 'application/javascript',
+      ]
+    ), -2000, $event->region);
+  }
 }
 
 /**
-- 
GitLab