Advanced Theming
Techniques
Trevor Twining & Bevan Rudge
4 March 2009�DrupalCon DC 2009
Follow along http://tinyurl.com/dcdc-theming
Trevor Twining
Trevor works on themes for some of CivicActions' largest sites; Amnesty International, WITNESS Video Hub. Trevor has been working with CivicActions for almost a year, and works from his family home in Niagara, Canada.
Bevan Rudge
Bevan has been theming Drupal for 3 years, and theming other web frontends for longer than that. Bevan has been working with CivicActions for over 1.5 years, and works from his home office in Christchurch, New Zealand.
CivicActions
CivicActions is one of the largest Drupal-specific shops around, with 30 to 40 internet professionals in about 10 different countries, 10 different time zones, covering 8 different languages.
Some of CivicActions' clients
CivicActions is changing the world, one node at a time, by empowering non-profits, NGOs and progressive political organisations with powerful internet tools for the modern web.�
Anatomy of a Theme
.info files
Perfect place to
Template overrides
Function overrides
Theming at module level
Needed for theming new functionality.
Overriding
It's what Drupal theming is all about
How to override
How to override
Overriding Template Files
HINT: Use theme developer module to find where the original template file is.
Overriding Template Files
Work out how to use NAME_preprocess_HOOK() instead!
Preprocess functions are better than template files!
DEMO (1 minute): How to use a preprocess function instead of a template file
How to override
Overriding theme functions
Lots of duplicate code!
Preprocessing theme functions
There is no preprocess for theme hooks, but...
Preprocessing theme functions
There is no preprocess for theme hooks, but you can modify the function arguments, then call the original theme function. E.g.�// Overrides theme_links()�// http://api.drupal.org/api/function/theme_links/6�function ca09_links($links, $attributes = array('class' => 'links')) {� // Do something to $links or $attributes here� return theme_links($links, $attributes);�}�
DEMO? (1 minute): How to pseudo-preprocess a theme function.
Preprocessing theme functions
Warning!
A module may have already overridden the original theme_HOOK() function.�
Find out from the theme registry or test with function_exists()�
Subtheming
it just makes theming easier
Why a subtheme?
In Drupal 6, subthemes go in sites/default/themes/ and not sites/default/themes/zen/
Use Zen for subtheming
Using Zen's body classes
// Preprocesses variables for page.tpl.php
function ca09_preprocess_page(&$vars) {
$body_classes = explode(' ', $vars['body_classes']);� // Add or remove some classes from $body_classes (an array)� $vars['body_classes'] = implode(' ', $body_classes);�}
DEMO? (1 minute): adding more body classes.
Theme Settings
Custom Theme Settings
Custom Theme Settings
Avoiding code bloat
Keep your theme sustainable, extensible, maintainable
Code bloat costs you more $$$ when you need to change stuff
It makes it harder for another person to jump in and take over a theme.
How to avoid code bloat
How to avoid code bloat
How to avoid code bloat
Theme Registry
Theming forms
Theming forms
Theming forms
Modules for theming
This is where the Dark Art of Theming meets the Black Magic of Module development
Module Development
This is where the Dark Art of Theming meets the Black Magic of Module development
See api.drupal.org, and learn Drupal module development.�
Resources that help you theme faster
Resources
Useful contrib modules for themers
Firebug
Firebug is my primary theme development too, even before my text editor. You can almost use firebug for prototyping.
DEMO (2 minutes): Using firebug for the above tasks.
Questions