Borja Borja - 5 months ago 24
PHP Question

Add css and js only for other "page--node--x.tpl.php" in Drupal 7?

i would like to have other template for only page (in my case node/348), so i write that i must create other

page--node--348.tpl.php


but now how can add other external css and js file ? where ?

Maybe in template.php ?

Thanks a lot and sorry for my english :)

Answer

Make a hook_node_view in one of your custom module, then check the node id is 348 and inject your css and js like this :

function nameOfYourModule_node_view($node, $view_mode, $langcode) {
  if ($node->nid == 348) {
    $node->content['#attached']['js'][] = array
    (
      'type' => 'file',
      'data' => path_to_theme() . '/js/my_script.js',
      'group' => JS_THEME,
      'preprocess' => TRUE,
      'scope' => 'footer',
      'weight' => '999',
    );
  }
}