Mikkel Høgh

Snippet: node type specific CSS files

Friday, 2nd of January, A.D. 2009

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.).

Mikkel Høgh

My name is Mikkel Høgh, I've worked with web tech for the last 20 years. These days, I work with e-commerce in Central Switzerland.