Mikkel Høgh

Coding the web since 1999

02 Jan 2009

Snippet: node type specific CSS files

I just wrote a fun little bit of code for answering a question on our Danish Drupal community site.

The user in question wanted to add different CSS-files, depending on the type of the node used as the main page content, and I just wanted to share a little bit of code demonstrating how much can be a achieved very easily with Drupal’s very flexible theming system.

So with no further ado, here it is:

  * Override or insert variables into the node templates.
function craftsmen_preprocess_node(&$vars, $hook) {
  if ($vars['page'] && // The node is the main page content
      $vars['type'] == 'side') { // Node type is 'side'
    drupal_add_css(path_to_theme() . '/node-side.css', 'theme');

And in the example here, “craftmen” is the name of the theme (the one used on Reveal IT, actually), and “side” is the node type (it means “page” in Danish.).