I cannot replace a JavaScript file
-
29-09-2019 - |
Question
I want to remove a JavaScript file from my Drupal website, and add a custom one. This is the code I'm using in my template.php file, but it doesn't work (the old js file is still loaded). (I've cleared the cache)
function zen_preprocess_page(&$vars, $hook) {
/*update js file*/
$scripts = drupal_add_js();
unset($scripts['module']['sites/all/modules/lightbox2/js/lightbox.js']);
$scripts['module']['sites/all/themes/zen/zen/myjs/lightbox.js'] = array('preprocess' => 1, 'cache' => 1);
$variables['scripts'] = drupal_get_js('header', $scripts);
Solution
I tested your code, and it actually works, if you place it in hook_preprocess_page()
. Your problem is probably that you use the wrong proprocess function, so that it doesn't effect the $scripts variable you use in your page template, or that is it overridden somewhere later in the process.
Edit
- You should not edit the Zen theme directly, but sub theme it instead.
- Have you tried adding the JavaScript file with
drupal_add_js()
? - Have you inspected
$variables['scripts']
after changing it to see if it was as expected?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow