[development] Overriding CSS files in themes

Khalid B kb at 2bits.com
Wed Nov 1 01:26:06 UTC 2006

Hello all.

Many modules have their own mymodule.css or mymodule.js

Most of these are added via:

theme_add_style(drupal_get_path('module', 'mymodule')).'/mymodule.css');

This is often called in hook_menu() so as to get in as early as possible.

Themes almost always need to override those css files. The way it is done
is to go to the mymodule directory and edit mymodule.css.

The problem here is when one upgrades, they have to be very careful
not to overwrite the changes. What is worse, if one is upgrading from
cvs directly, then there could be conflicts resulting in invalid data in
the file.

So, in order to make this easier for all, it is best if this is written as:
theme('add_style', drupal_get_path('module', 'mymodule')).'/mymodule.css');

This way, the theme can say:

function mytheme_add_style($path) {
  $file = basename($path);
  theme_add_style(base_path() . path_to_theme() . '/' . $file);

This way, the css file for each module has a copy in the theme directory that
can be customized, and and the rest of the stuff does not get touched.

Some modules do this already, most do not.

Here is the status in 4.7 today:

$ grep "theme.*(.*add_style" */* | awk -F: '{print $1}' | sort -u | wc -l

$ grep "theme_add_style" */* | awk -F: '{print $1}' | sort -u | wc -l

So, module owners should be encouraged to use:

theme('add_style', ...)

instead of

theme_add_style( ...)

Is it enough that I post here, or is there some other mechanism (short
of opening 77 issues for 77 modules ...)

More information about the development mailing list