0

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 :)

3 Answers 3

1

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',
    );
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What do you think for "custom module" ? maybe the template.php file ?
Drupal themes can only implement theme functions (which include template preprocess and process functions) or alter hooks. If you have never created a module it is a good time to start : drupal.org/developing/modules/7 All you need is a directory with the name of your module, and a .module and .info file with the name of your module. In your .module you will just have the function I described.
1

You can simply add a condition in "template_preprocess_html()" function in your "template.php" as below:

<?php
    function THEME_NAME_preprocess_html(&$variables) {
      $arg = arg();
      $nid = 348;
      if ($arg[1] == $nid) {
        drupal_add_css(path_to_theme() . '/css/my_style.css');
        drupal_add_js(path_to_theme() . '/js/my_script.js');
      }
    }

?>

1 Comment

Using drupal_add_js and drupal_add_css is frowned upon in drupal >= 7.x, to the point it disappeared in drupal 8. The main reason is linked to how cache engines are saving pages. So please stop using it in drupal 7.
0

You can use module CSS injector and JS to custom ONLY the node that you want:

https://www.drupal.org/project/css_injector

https://www.drupal.org/project/js_injector

about js, you can create a new js and custom with "id" and "class" of your node. With css injector you can anable the changes only for the node that you want.

1 Comment

If the only purpose of adding this module is to add one js and css, better write the code yourself. The less modules installed the better on perfs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.