- Issue created by @ishwar
- 🇺🇸United States luke adams
I know this post is a little old, however, if you're just trying to load the jquery.min.js file from Google's CDN, the following will work. Can be placed in any custom module you've got.
/** * Implements hook_js_alter(). */ function MY_MODULE_js_alter(&$javascript) { $module_handler = \Drupal::service('module_handler'); $advagg_exists = $module_handler->moduleExists('advagg_cdn'); if ($advagg_exists) { return; } // Modified from advagg_cdn module. This module was removed from 6.0 version. // Get JQuery from Google cdn. $jquery_lib = 'core/assets/vendor/jquery/jquery.min.js'; if (!empty($javascript[$jquery_lib])) { $url = 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'; $javascript[$jquery_lib]['type'] = 'external'; $javascript[$jquery_lib]['data'] = $url; $javascript[$jquery_lib]['version'] = '3.5.1'; } }
- Status changed to Needs review
5 months ago 8:10am 5 August 2024 - 🇮🇳India vinai_katiyar Delhi NCR
Hi @ishwar,
The submodule AddAvg CDN is removed under the release 6.0.0-alpha1 → version.
To resolve their dependency in Drupal10 , you can use hook_js_alter() and hook_css_alter().Example Using hook_js_alter()
<?php /** * Implements hook_js_alter(). */ function my_module_js_alter(&$javascript) { // Replace with your CDN URL $cdn_url = 'https://your-cdn.com'; // Target specific JavaScript file $jquery_lib = 'core/assets/vendor/jquery/jquery.min.js'; // Check if the specific JavaScript file is set if (!empty($javascript[$jquery_lib])) { // Replace the local path with the CDN path $javascript[$jquery_lib]['data'] = $cdn_url . '/jquery.min.js'; } } ?>
Example Using hook_cs_alter()
<?php /** * Implements hook_css_alter(). */ function my_module_css_alter(&$css) { // Replace 'https://your-cdn-domain.com' with your actual CDN domain $cdn_url = 'https://your-cdn-domain.com'; // Target a specific CSS file in your module $my_css_file = 'modules/custom/my_module/css/my_styles.css'; // Check if the specific CSS file is set if (isset($css[$my_css_file])) { // Replace the local path with the CDN path $css[$my_css_file]['data'] = $cdn_url . '/path/to/your/css/file.css'; } } ?>