Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am trying to add a CSS and JS files when a certain content type is being viewed (Drupal 6). The problem is that the code is being added to every page.

I have tried this first, included in template.php:

function phptemplate_preprocess_node(&$vars, $hook) {
  $node = $vars['node'];
  if ($node->type == 'mytype') {
        drupal_add_css(path_to_theme() . "/css/mytpess.css");

        drupal_add_js(path_to_theme() . "/javascripts/jquery.masonry.min.js");
        drupal_add_js("$(document).ready(function(){
                          $('.node').masonry({
                            itemSelector: '.field-type-flexifield'
                          });
                        });",'inline');
  }
}

Then I moved it out of there and placed it in node-mytype.tpl.php:

drupal_add_css(path_to_theme() . "/css/mytpess.css");

drupal_add_js(path_to_theme() . "/javascripts/jquery.masonry.min.js");
drupal_add_js("$(document).ready(function(){
                  $('.node').masonry({
                    itemSelector: '.field-type-flexifield'
                  });
                });",'inline');

But either way it gets added to every page. How can I resolve this?

share|improve this question
2  
I see nothing wrong with that code. have you cleared your site cache lately? – jdu Aug 22 '12 at 19:06
There's nothing in either snippet that is telling the script to only be added on the front page. – Chapabu Sep 11 '12 at 10:55
I don't think he wants it to be added to the front page :). – hampusn Sep 11 '12 at 10:57
Aaah, silly me! I'm not sure WHERE I got front page from :-p – Chapabu Sep 11 '12 at 10:59
Dumb question - have you cleared the theme registry? – Chapabu Sep 11 '12 at 11:00
show 1 more comment

3 Answers

If you are adding drupal_add_css or_js in phptemplate_preprocess_node,

  1. First fetch all the added css in one variable and assign this for $styles value
  2. Same like css you need to do for js also.

Here with see the sample code below

function phptemplate_preprocess_node(&$vars, $hook) { $node = $vars['node']; 
if ($node->type == 'mytype') {
    drupal_add_css(path_to_theme() . "/css/mytpess.css");

    drupal_add_js(path_to_theme() . "/javascripts/jquery.masonry.min.js");
    drupal_add_js("$(document).ready(function(){
                      $('.node').masonry({
                        itemSelector: '.field-type-flexifield'
                      });
                    });",'inline'); 
 }
$css = drupal_add_css();
$scripts = drupal_add_js();
$vars['styles'] = drupal_get_css($css);
$vars['scripts'] = drupal_get_js('header', $scripts);
}
share|improve this answer
$vars['styles'] = drupal_get_css(); should do it. – Citricguy Sep 26 '12 at 4:20
drupal_add_css() return array of css added in that site. – ramesh babu Oct 19 '12 at 13:17

You can use http://api.drupal.org/api/drupal/developer!hooks!node.php/function/hook_view/6 hook_view in custom module to do this.

For example if your module name is "example" then function should be

function example_view($node, FALSE, TRUE) {
  if ($node->type == "mytype") {
    drupal_add_css(path_to_theme() . "/css/mytpess.css");
    drupal_add_js(path_to_theme() . "/javascripts/jquery.masonry.min.js");
  }
}
share|improve this answer

Isn't template_preprocess_node() too late? Try using template_preprocess_page() instead. $variables['node']->type as usual.

function template_preprocess_page(&$vars) {
  if (isset($vars['node'], $vars['node']->type) && $vars['node']->type === 'mytype') {
    //ADD_JS
    //ADD_CSS
  }
}

Edit: as a sidenote, template_preprocess_page should be renamed to THEME_NAME_preprocess_page or MODULE_NAME_preprocess_page. Depending on where you place that function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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