0

Adding in the child theme a CSS file with the same name as the parent theme CSS file overrides the parent file. It doesn't seem to work JavaScript files.

How can I override a JavaScript file loaded from the parent theme?

7
  • 1
    Have you tried to replace it using hook_js_alter api.drupal.org/api/drupal/…
    – berramou
    Commented Nov 21, 2019 at 14:21
  • cannot find the js from parent theme when printing $javascript ? (using the D7 hook)
    – Matoeil
    Commented Nov 21, 2019 at 14:49
  • Did you try examples from api.drupal.org/api/drupal/modules%21system%21system.api.php/…, are you using advagg module or other module for js aggregation ?.
    – berramou
    Commented Nov 21, 2019 at 14:52
  • 1
    Generally you don't want to replace a JS file completely because it's very unlikely that you will want to change everything it does. Can you explain why you want to get rid of this one? Commented Nov 21, 2019 at 14:52
  • 1
    and if the js file contain behavior, you should override the behavior with the same name in your child theme .
    – berramou
    Commented Nov 21, 2019 at 14:59

2 Answers 2

1

You can use hook_js_alter to replace your JS file (from parent theme) with the one in your child theme.

function child_theme_name_doc_js_alter(&$javascript) {
  $javascript['themes/parent_theme_name/js/parent_theme_name_navigation.js']['data'] = drupal_get_path('theme', 'child_theme_name') . '/js/child_theme_name_navigation.js';
}
2
0

in the theme.info

libraries-override:
  parent_theme_name/library_name:
    css:
      base:
        ; deactivate a file
        css/parent_theme_name_language_switcher.css: false
    js:
      ; override a file
      js/parent_theme_name_navigation.js: js/child_theme_name_navigation.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.