I am trying to add JavaScript and CSS files to one node type page from my template.php file but it is not working. Here is my template.php code:

// $Id: template.php,v 1.13 2010/12/14 01:04:27 dries Exp $

function mttheme_preprocess_html(&$variables) {
  $node = node_load(arg(1));
  if ($node->type = 'sales_team_page') {
      array('type' => 'file', 'scope' => 'header')
      array('type' => 'file', 'scope' => 'header')
      array('type' => 'file', 'scope' => 'header')

Does anyone know why it is not working?

  • this belongs in Drupal Answers!
    – Alex Weber
    Jun 2 '11 at 21:23

Since you want your CSS and JavaScript when displaying a node, you can add the files it in the preprocess function for the node template. Also, as stated in their documentation, when adding a file, the first argument for drupal_add_css() and drupal_add_js() is the path the CSS/JavaScript file relative to the base_path(). You can use drupal_get_path('theme', 'THEME') to get the path of your theme relative to the base path.

function THEME_preprocess_node(&$variables) {
  if ($variables['view_mode'] == 'full') {
    $node =& $variables['node'];
    if ($node->type == 'sales_team_page') {
      $path = drupal_get_path('theme', 'THEME');
      drupal_add_css($path . '/misc/jquery.fancybox-1.3.4.css');
      drupal_add_js($path . '/misc/jquery.mousewheel-3.0.4.pack.js', array('type' => 'file', 'scope' => 'header'));
      drupal_add_js($path . '/misc/jquery.fancybox-1.3.4.pack.js', array('type' => 'file', 'scope' => 'header'));
      drupal_add_js($path . '/misc/jquery.fancybox-calls.js', array('type' => 'file', 'scope' => 'header'));

Also, doing $node = node_load(arg(1)); to load the currently viewed node is unsafe and wrong. If you have a non node page at whatever/42 but have an existing sales_team_page node with nid = 42, you will load this node and add your CSS and JavaScript files even if the user is not viewing the node.

  • any idea how to add #attached['js'] here ? Feb 11 '16 at 13:17
  • You can't use #attached here. In order to use #attach you need to be able to alter the renderable array before it is rendered through drupal_render(). For rendered node, this can be done in hook_node_view() or hook_entity_view(). Feb 11 '16 at 16:09

Probably because you dont get the node type correctly. Use preprocess node in order to capture the node type correctly.

function mttheme_preprocess_node(&$variables) {
 if ($variables['type'] == "sales_team_page"){
   drupal_add_css(drupal_get_path('theme', 'mttheme') . 'misc/jquery.fancybox-1.3.4.css');
   //add js in the same way using drupal_get_path()

You can also add them through your .info file of your theme :

name = Your theme
core = 7.x
engine = phptemplate

scripts[] = my_js.js
  • I only want to add it to one page, I do not want it to appear on every page. That is why I do not use the .info file. I tried changing the code to what you have but it still doesnt work...
    – zach
    Jun 2 '11 at 21:48
  • hmmm from what i see a lot changed in d7.In order to get node type in preprocess_html you need a lot of stuff....just do a var_dump to $variables to see what it gets to get to content type. You could also check bartik's template.php to see how it adds js. You probably need the full path not the relative, using path_to_theme()
    – rootatwc
    Jun 2 '11 at 22:03
  • @zach edited accordingly.i hope this helps now
    – rootatwc
    Jun 2 '11 at 22:15
  • It is not working. At this point I am starting to think that my theme never reads template.php. Could there be something wrong with that file? Do I have to do anything else besides place the code in the template.php file? I have it in the root of my sites/all/themes/mytheme/ folder. I also started it with <?php and left it open. What else do I need to do?
    – zach
    Jun 3 '11 at 14:53
  • you could clear your cache from performance.. cache can be a bitch a lot of times..if clearing cache wont work as well something else fails... maybe your content-type is not called 'sales_team_page'? Also get drupal.org/project/devel_themer .. it really helps to drupal theming Jun 3 '11 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.