Snippet: node type specific CSS files

Published on Fri, 2009-01-02 - 21:45

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

Tags
The comments for this post are now closed. If you have corrections, suggestions, or other feedback, you can reach me through the contact page.