From 5d1754609dd6fad0e2bcce372ff4fb6af22a6499 Mon Sep 17 00:00:00 2001
From: Coleman Watts <coleman@civicrm.org>
Date: Wed, 5 Jun 2019 20:54:47 -0400
Subject: [PATCH] Conditionally use Drupal's jQuery only if available

---
 js/crm.drupal8.js | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/js/crm.drupal8.js b/js/crm.drupal8.js
index ea8dadd79f6..91c7080fbe2 100644
--- a/js/crm.drupal8.js
+++ b/js/crm.drupal8.js
@@ -17,13 +17,16 @@ localStorage.setItem('Drupal.toolbar.activeTabID', JSON.stringify('toolbar-item-
 
   // Wait for document.ready so Drupal's jQuery is available to this script
   $(function($) {
-    // Need Drupal's jQuery to listen to this event
-    jQuery(document).on('drupalToolbarTabChange', function(event, tab) {
-      if (CRM.menubar && CRM.menubar.position === 'below-cms-menu') {
-        var action = jQuery(tab).is('#toolbar-item-civicrm') ? 'show' : 'hide';
-        CRM.menubar[action]();
-      }
-    });
+    // If Drupal's jQuery isn't loaded (e.g. on a stripped-down front-end page), we don't need to worry about the toolbar
+    if (window.jQuery) {
+      // This event is only triggered by Drupal's copy of jQuery. CRM.$ won't pick it up.
+      jQuery(document).on('drupalToolbarTabChange', function (event, tab) {
+        if (CRM.menubar && CRM.menubar.position === 'below-cms-menu') {
+          var action = jQuery(tab).is('#toolbar-item-civicrm') ? 'show' : 'hide';
+          CRM.menubar[action]();
+        }
+      });
+    }
   });
 
 })(CRM.$);
-- 
GitLab