Haven't used this module yet to solve the problem, but wanted to suggest my current solution as an addition.

function cpn_preprocess_node(&$vars) {
  $identifier = $vars['node']->nid;
  if (module_exists('i18n')) {
    $identifier = $vars['node']->vid;
  }
  $file = path_to_theme() .'/css/nodes/'. $identifier .'.css';
  if (file_exists($file)) {
    $age = date("U", filemtime($file));
    drupal_add_css($file.'?v='.$age , 'theme', 'all', FALSE);
  }
}

Comments

doublejosh’s picture

I added the age addition when I posted this without testing, when in fact you can't include query strings in drupal_add_css, drupal_add_link, etc. Additionally, fixed the problem of multiple include attempts with a static flag.

function cpn_preprocess_node(&$vars) {
  // Add node specific CSS files with a file age version query tag.
  $identifier = $vars['node']->nid;
  if (module_exists('i18n')) { // Allow sharing CSS between translations.
    $identifier = $vars['node']->vid;
  }
  static $addNodeCSS;
  if(!$addNodeCSS[$identifier]) { // Prevent multiple adds.
    $file = path_to_theme() .'/css/nodes/'. $identifier .'.css';
    if (file_exists($file)) { // Only if file exists.
      $age = date("U", filemtime($file));
      $data = '<link type="text/css" rel="stylesheet" media="all" href="/'. $file .'?v='. $age .'" />';
      drupal_set_html_head(); // Extra for line break. See D6 limitations for query strings in add_css(), etc.
      drupal_set_html_head($data);
      $addNodeCSS[$identifier] = true;
    }
  }
} 
doublejosh’s picture

Here's a content type one too. Though if you use Adv Agg the bundler will do this for you.

function cpn_preprocess_node(&$vars) {
  // Add type specific CSS file with a file age version query tag.
  static $addTypeCSS;
  if(!$addTypeCSS[$vars['node']->type]) { // Prevent multiple adds.
    $typeFile = path_to_theme() .'/css/content-types/'.$vars['node']->type .'.css';
    if (file_exists($typeFile)) { // Only if file exists.
      $age = date("U", filemtime($typeFile));
      $data = '<link type="text/css" rel="stylesheet" media="all" href="/'. $typeFile .'?v='. $age .'" />';
      drupal_set_html_head(); // Extra for line break. See D6 limitations for query strings in add_css(), etc.
      drupal_set_html_head($data);
      $addTypeCSS[$vars['node']->type] = true;
    }
  }
}
doublejosh’s picture

Corrected a problem with the translated identifier, added node specific JS, and cleaned up a bit.
Realize that one goal of this module is to remove the node specific CSS/JS away from the theme, but sometimes it's appropriate.

/**
 * Attach extra assets for specific nodes/types.
 */
function cpn_preprocess_node(&$vars) {

  // Add node specific CSS files with a file age version query tag.
  $identifier = $vars['node']->nid;
  if (module_exists('i18n') && $vars['node']->tnid) { // Allow sharing CSS between translations.
    $identifier = $vars['node']->tnid;
  }
  static $addNodeCode;
  if (!$addNodeCode[$identifier]) { // Prevent multiple adds.
    $fileCSS = path_to_theme() .'/css/nodes/'. $identifier .'.css';
    $fileJS = path_to_theme() .'/js/nodes/'. $identifier .'.js';
    if (file_exists($fileCSS)) {
      $age = date("U", filemtime($fileCSS));
      $data[] = '<link type="text/css" rel="stylesheet" media="all" href="/'. $fileCSS .'?v='. $age .'" />';
    }
    if (file_exists($fileJS)) {
      $age = date("U", filemtime($fileJS));
      $data[] = '<script type="text/javascript" src="/'. $fileJS .'?v='. $age .'" charset="utf-8"></script>';
    }
    if(isset($data)) {
      foreach ($data as $d) {
	drupal_set_html_head(); // Extra for line break. See D6 limitations query in add_css() and add_link().
	drupal_set_html_head($d);
      }
    }
    $addNodeCode[$identifier] = true;
  }

  // Add type specific CSS file with a file age version query tag.
  static $addTypeCSS;
  if (!$addTypeCSS[$vars['node']->type]) { // Prevent multiple adds.
    $typeFile = path_to_theme() .'/css/content-types/'.$vars['node']->type .'.css';
    if (file_exists($typeFile)) {
      $age = date("U", filemtime($typeFile));
      $data = '<link type="text/css" rel="stylesheet" media="all" href="/'. $typeFile .'?v='. $age .'" />';
      // Extra for line break. See D6 limitations for query strings in add_css(), etc.
      drupal_set_html_head();
      drupal_set_html_head($data);
    }
    $addTypeCSS[$vars['node']->type] = true;
  }

}

My use case is having to create various one-off pages for marketing purposes.

doublejosh’s picture

In case anyone stumbles upon this, here's another solution: http://drupal.org/project/context_addassets

damienmckenna’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Active » Needs work

If this is to be included then it needs to be handled differently:

damienmckenna’s picture

Issue summary: View changes

remove debug line