Disable the drupal.css

I am sure everyone has had a time when using drupal that you have had to create a page and needed to alter the output that may be given by a Drupal module. A lot of the time this leads to much frustration cursing and in the end some sort of core hacking or something of that nature as well as cursing and hours of confusion.

Introduction

I think that is one of the subtle failures by drupal is getting all the power it has harnessed and being able to easily access resources on certain topics. Yet once you manage to get into the details it can be pretty sweet.

In this case there is a bit that needs to be understood about themeable functions, and for that be sure to look here for the basics of theming and what it entails. This is more a practical example.

So the first thing that you will need to do is find out what functions are themeable and what the default formatting of these functions is so that you can get both the default layout and the parameters each function may take. So to do this check out the Drupal API for a full list of the built in themeable functions. If you ever have time it is advisable to sit down and take a look at the functions here, some will definitely offer a better understanding of the functionality of the CMS but also give you many flexible tools to work with in module and theme design.

In this case we are going to have custom breadcrumbs. So we need to know what the function is so that we can overide it, and in our case it is the theme_stylesheet_import function which you can find here. Once you have taken a peek at the notes you can go ahead and fire up you favorite ide and navigate to your current active theme on the web server. So for example, if your current theme is called “Garland” and the theme folder is named millions then the path you would find the template.php file is site_root/themes/garland/template.php. Once we have this open you will be able to see the default functions and overrides for the current theme, garland one of drupal 6′s default themes is a very good example for this.

Code

Once we the active template.php is open scroll down to the bottom of the page and prepare to paste in the function from theme_stylesheet_import from Drupal into you file. Now you have the base in which to work off of, so the next step is to rename the function that you have just added to something drupal will be able to interpret.

At this point you will likely need to have a better understanding of templating systems, and the available templating systems in drupal and you can find this piece of good reading here . So to save you from the reading the current default in drupal is PHPTemplate. Now why does this matter? Well if we are going to override the themeable function we should prefix this with the current template engine name, in our case the function theme_stylesheet_import would be name phptemplate_stylesheet_import in out template.php file.

function phptemplate_stylesheet_import($stylesheet, $media = 'all') {
if (strpos($stylesheet, 'misc/drupal.css') !== 0) {
$stylesheet = str_replace('misc/drupal.css', 'misc/mysite.css', $stylesheet);
}

}

Now to be sure that this works it is best to clear the drupal cache, it has happened before where you make changes to a theme file but they are not readily available when you go back to the browser and refresh. This is likely because you have caching turned on or you need to clear the cache. One way to do that is to uninstall and reinstall the theme. This is because each time that you install a theme in drupal it will go back and clear the cache and start to rebuild for the theme as if it was installed for the first time. To do this login to the admin and go to admin/build/themes, uncheck the theme and select another as default and save, when the page reloads, you can set check your old theme as default and there we go. Now when you go ahead and load your site you will see that the drupal.css file is no longer loaded.

Conclusion

Well that is great but what is this good for other than that. Well perhaps you have a site where you need to load different stylesheets for each section of your site for some reason or don’t want people to be able to detect you are on drupal. one way would be to modify the phptemplate_stylesheet_import function that we just wrote and replace the drupal.css with a file of our own like this:

function phptemplate_stylesheet_import($stylesheet, $media = 'all') {
if (strpos($stylesheet, 'misc/drupal.css') !== 0) {
$stylesheet = str_replace('misc/drupal.css', 'sites/themes/divninja/ninja.css', $stylesheet);
}
if (strpos($stylesheet, 'misc/drupal.css') === 0) {
return theme_stylesheet_import($stylesheet, $media);
}
}

Further Reading

  1. Overriding themable output
  2. Remember Always practice safe coding, never hack the core
theme_stylesheet_import