2

My attempt to remove CSS and JS assets is not working. I have placed the following declarations in my module's *.info.yml file. But after clearing the cache, I still see the assets in the head of the document. Is there anything wrong with my syntax:

libraries-override:
  global-styling:
    css:
      base:
        /themes/stack/css/lightbox.min.css: false
    js:
      /themes/stack/js/lightbox.min.js: false

Here is the relevant Drupal documentation: https://www.drupal.org/docs/8/theming/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-theme#override-extend

I also tried these for the key:

  • stack/global-styling
  • stack.global-styling

I also tried it without the full paths:

libraries-override:
  stack/global-styling:
    css:
      base:
        lightbox.min.css: false
    js:
      lightbox.min.js: false

Nothing is working so far.

3
  • 1
    I've not tested this, it is just a guess: Given the loading and dependency chain (themes can add dependencies on modules, but not vice versa; modules are always loaded before themes) I don't think you can disable theme assets from within a module.
    – Hudri
    Mar 16, 2020 at 6:55
  • You cannot use libraries-override in modules, only in themes. This is because theme assets are added in core then modules, and are altered in themes. It's up to the theme to remove files. That's the Drupal paradigm.
    – Jaypan
    Mar 17, 2020 at 3:53
  • @Jaypan Can you comment on whether I have the syntax correct in either of the two samples I presented in the question. I get "Incomplete library definition" errors when I move the declarations into the theme. I have tried varying the syntax many different ways and can't find anything that works, whether I put the declarations in a module or in my theme. Thanks!
    – arnoldbird
    Mar 17, 2020 at 14:28

1 Answer 1

0

This doesn't answer the original question, but turns out to be a better solution in my case. You must clear your cache with each change to this function:

/**
 * Implements hook_library_info_alter().
 */
function example_library_info_alter(&$libraries, $extension) {

  // If you have 'stack' as your subtheme, you might have unwanted lightbox assets.
  if ('stack' === $extension) {

    if (isset($libraries['global-styling'])) {

      $current_path = \Drupal::service('path.current')->getPath();
      $path_args = explode('/', $current_path);

      // Your conditions may vary.
      if ('book' == $path_args[1] && isset($path_args[2]) && is_numeric($path_args[2])) {

        // Helper function below.
        if (example_array_key_exists_r('css/lightbox.min.css', $libraries)) {
            unset($libraries['global-styling']['css']['base']['css/lightbox.min.css']);
        }

        if (example_array_key_exists_r('js/lightbox.min.js', $libraries)) {
            unset($libraries['global-styling']['js']['js/lightbox.min.js']);
        }

      }
    }
  }

}

/**
 * Recursively search for an array key.
 *
 * @see https://stackoverflow.com/a/2948985/861826
 *
 */
function example_array_key_exists_r($needle, $haystack) {

  $result = array_key_exists($needle, $haystack);

  if ($result) return $result;

    foreach ($haystack as $v) {
      if (is_array($v)) {
        $result = example_array_key_exists_r($needle, $v);
      }
      if ($result) return $result;
    }

    return $result;
}

Your Answer

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

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