You are here
Home ›Drupal Theming: Don't hack, use a sub-theme
I had thought using sub-themes was standard practice in the Drupal world, but I keep coming across sites where someone has taken a Drupal contrib theme, and hacked it to suit their purposes. If you’re thinking about hacking a contrib theme, I recommend you stop and have a good hard think before you do so. It may save future-you a lot of work.
OK. I know. We’ve all done it. Either we didn’t know Drupal had a sub-theme feature, or it just seemed like too much work at the time when we only wanted to tweak a few colours here and there. But after inheriting a few of these projects now, I can tell you that it’s really not a good long-term strategy. A sub-theme is a much better approach.
The beauty of using a sub-theme, is that instead of directly changing the contrib theme’s files, you just override the bits that you need to. This is especially handy if you only need to change a few CSS rules and the odd image. Using this approach, when the hard-working Drupal security team finds a vulnerability in the base theme, you can just download the update to the base theme, check that everything looks OK, and carry on your merry way—as opposed to messing about with back-up copies and patches if you hacked the base theme directly.
Unfortunately, there aren’t a whole lot of well-written, detailed tutorials on how sub-themes work. There are a number of tutorials around though, so if you’re looking for some help to get started, try:
In summary, to create a sub-theme, you create a new theme .info file as if you were going
to create a theme from scratch. Then you add a line like the following to make it a sub-theme:
base theme = base_theme_name
Your theme will now inherit stylesheets and templates from the base theme. To add your own
css, create a file in your theme’s directory called mytheme.css (or something more appropriate), and add the following line
to your .info file:
stylesheets[screen][] = mytheme.css
This new stylesheet will now be added to the page after any other stylesheets for the screen media type, and you can override styles to your heart’s content.
It’s really not that difficult to create a sub-theme, and any developers who come later will (at least, they should) thank you.



Comments
Very good
Add new comment