1

If I try to add a css file using drupal_add_css to a custom module where there is already a css file loaded with the same name elsewhere (in my case in the theme, based on Zen), the module version does not load at all. Nothing to do with overriding styles - the two files contain completely different style information. If I change the filename in the custom module to something else which does not conflict, it loads correctly.

The question is is this by design? If not, is it a known bug? In my opinion this should not happen as a module developer has no idea what css files will be called in any custom theme.

6
  • 1
    I think there's some kind of guidelines that you should name your css and javascript files with your module name as a prefix. Something like modulename_styles.css or modulename_scripts.js Commented May 15, 2012 at 10:35
  • Yes someone else has just informed me of this. I suppose this could still be an issue if the theme and module have the same name... unlucky but possible. Thanks Commented May 15, 2012 at 10:44
  • If your module and theme have the same name, you will be having much bigger problems than just overridden stylesheets.
    – sheena_d
    Commented May 15, 2012 at 13:46
  • @sheena_d Is it really beyond the realms of possibility that you have a theme called, for example, Awesome and a module called Awesome, thereby having the same prefix? You might not create one or the other to conflict with a module/theme you have but if you make it publicly available you can't guarantee to not clash with any existing theme/module surely. I'm being devil's advocate a bit here I admit. Commented May 21, 2012 at 15:03
  • You could have a theme called awesome_theme and a module called awesome_module, but if both the theme and module are named awesome, you will run the risk of conflicts within Drupal core and within your theme and module when calling hooks, etc. As for not being able to "guarantee to not clash with any existing theme/module," drupal.org will not let two projects (independent of type) exist with the same name. It is definitely possible for a module and theme to exist in a site with the same name, I have done it before. Once. Then I learned the hard way that doing this can cause problems.
    – sheena_d
    Commented May 21, 2012 at 15:31

1 Answer 1

2

Looking at the drupal_add_css() function, it seems this behaviour is by design.

For any CSS that you add using the function without using 'inline' as an argument, the array of files is keyed by the first argument (string) that you pass in. So if you were to run:

drupal_add_css('file.css');

And another module has also called drupal_add_css() with the same file name, the original will be overwritten.

As Marius Illie mentions in a comment above, the best way to avoid this is to prefix any CSS files you add with your module name.

1
  • Yes a module prefix seems to be the accepted way of avoiding this. Thanks. Commented May 15, 2012 at 12:11

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.