1

I have a base theme that loads several libraries, some through the libraries.yml file and some loaded conditionally in a preprocess function. I also have a base theme with its own library loaded through libraries.yml.

This is the order they are loading in:

  1. Libraries loaded in base theme's libraries.yml
  2. Libraries loaded in sub theme's libraries.yml
  3. Libraries loaded in base theme's preprocess function

This is a problem because I need to override some CSS from the libraries loaded in the base theme's preprocess function in my subtheme. I want it to load like this:

  1. Libraries loaded in base theme's libraries.yml
  2. Libraries loaded in base theme's preprocess function
  3. Libraries loaded in sub theme's libraries.yml

They are loading in that order regardless of the CSS weights defined in the libraries file (base, theme etc.).

Is there anything I can do about this? This is the relevant code from the base theme, used in hook_preprocess_html().

  if (\Drupal::moduleHandler()->moduleExists('module')) {
    $variables['#attached']['library'][] = 'basetheme/library';
  }

Will libraries loaded through a preprocess function always load later than the libraries loaded through libraries.yml? Are there any resources that explain this? And as an aside, where would libraries loaded through twig go?

1
  • 1
    Sounds like you want to add a preprocess function to your own theme which loads the library containing overrides when the same condition as in the base theme matches. The sub-theme's hook will run after the base theme's
    – Clive
    Apr 19, 2018 at 19:03

1 Answer 1

3

Can you define your subtheme's library as extending or overriding your base theme's library. You'd have to do that in your subtheme's .info.yml. I think that should work. I think if you went this route you would just have a .css file in your subtheme that entends -- gets added whenever -- your basetheme's library is loaded. In this case you're override wouldn't be a "library" in your subtheme. Drupal should be smart enough to load your css file whenever the basetheme's library is loaded.

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.