0

I am trying to add a script code on all but few pages. I tried the following code on the template.php bfile, but it doesn't work.

 function THEME_preprocess_page(&$variables) {
   if (isset($variables['node']->type)) {
     $variables['theme_hook_suggestions'][] = 'page__' . 
     $variables['node']->type;
   }

   if ($variables['nid'] == '77') {
     drupal_add_js(drupal_get_path('theme', 'THEME') .'/scripts/path.js');
   }
 } 
  • I believe that should work. try to take the drupal_add_js out of the if statement and see if it adds it. If it doesnt then ensure the path is correct. Last ensure that the theme preprocess is actually firing off. – chadpeppers May 22 at 2:50
  • 3
    $variables['nid'] should probably be $variables['node']->nid – Jaypan May 22 at 3:51
  • There isn't any $variables['nid'] available. You get the node ID from $variables['node'] and you don't compare it with a string literal. – kiamlaluno May 22 at 13:41
0

You should try below solution as well. I have added JS & CSS for the specific node.

function YOURTHEME_preprocess_page(&$variables){
  if (array_key_exists('node', $variables)) {
    $node = $variables['node'];
    if ($node->nid == 8) {
      drupal_add_css(path_to_theme() . '/css/cdata.css', array('group' => CSS_THEME, 'media' => 'screen', 'every_page' => TRUE, 'browsers' => array('IE' => 'gte IE 9', '!IE' => FALSE), 'preprocess' => FALSE));    
      drupal_add_js(path_to_theme() .'/js/custom_records.js', 'file');
    }
  }  
}

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.