0

I'm trying to load a css file in a custom module, to style a template file I'm rendering. I followed the basic steps:

global-styling:
  version: 1.0
  css:
    layout:
      css/rr_users.css: {}
  dependencies:
    - rr_users/global-styling

and

name: RR users module
description: Generates editing pages for users
package: Custom
type: module
core_version_requirement: ^9
libraries:
  - rr_users/global-styling

I can't find out why this isn't working. It looks to me that I did it correctly. Also I already tried to look up a solution, but none of the found ones worked out for me.

I already tried to empty the cache etc.

1 Answer 1

0

libraries is not a valid key for a module .info.yml file; that key is for theme info files only. The full list of valid properties is available here.

There are a few different ways to attach libraries to a page from a module, depending on the requirement and available context. Those different methods are discussed, with examples, under Attaching a library to page(s) in the official docs.

Given the tags on the question, Attaching a library to a view might be of most interest to you:

/**
* Implements hook_views_pre_render().
*/
function yourmodule_views_pre_render(ViewExecutable $view) {
  if (isset($view) && ($view->storage->id() == 'super_awesome_view')) {
    $view->element['#attached']['library'][] = 'custom/custom_view';
  }
}
5
  • Sadly that's not working either, I changed the yourmodule into rr_users and did the storage->id() check on 'user_dashboard, which is the name of the twig file. I also put the method within the .moduke, as the docs tell you to '
    – Jorn Reed
    Jun 13 at 11:27
  • I use the same approach all the time so it definitely works, make sure you've named everything correctly and are checking the right things (e.g. you should be testing against the view ID, not the template file name, in case those are different)
    – Clive
    Jun 13 at 11:43
  • Ah okay, I managed to get it to work. I misspelled rr_users.libaries.yml, but I also had to do ['#attached']['library'][] = 'custom/custom_view'; in my controller method that renders that view. Idk why, but with that double approach of mine it works now.
    – Jorn Reed
    Jun 13 at 11:46
  • You definitely don't have to add it twice (the system is setup to de-dupe anyway so it wouldn't do anything). Seems likely you don't have the IDs or something else quite right in the preprocess method if you're having to inject it somewhere else. But aside from some really pedantic arguments about where the code should be, adding in the controller won't cause any problems
    – Clive
    Jun 13 at 11:53
  • Alright, I currently have it like this function nn_users_preprocess_page(&$vars) { $vars['#attached']['library'][] = 'nn_users/global-scripting'; } which is in the .module file. Probably fine for now
    – Jorn Reed
    Jun 13 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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