I am trying to add js by #attached, as I understand it's better from a caching perspective then drupal_add_js(). I try to use hook_page_alter() in template.php.
I have the following code:

function mytheme_page_alter(&$page) {
 $theme_path = drupal_get_path('theme', 'mytheme' );

  if (!isset($page['content']['#attached']))
      $page['content']['#attached'] = array
        'js' => array(),

  $page['content']['#attached']['js'][] = array($theme_path . '/test.js');

(this is adapted from http://www.jaypan.com/tutorial/including-javascript-or-css-files-every-page-load-module-drupal-7, I first tried to attach [#attached] directly to $page by means of $page['#attached'] = array... , but as I faced the problem described below I tried to test some code suggested by other people).

The problem is that the elements of the js are not properly set. After the 'js' => array(), The Aptana Debugger shows what I'd expect :

first dbug shot
After $page['content']['#attached']['js'][] = array($theme_path . '/test.js'); , however, the array values are not set:
debug 2
Still the value of array($theme_path . '/test.js')is what it 'should' be:
enter image description here

It's as if the ascription did not go through.
What did I do wrong?

  • Have no idea what your problem is. Your question is vague, what exactly is wrong and what are you expecting? Your first screenshot shows an empty js array, the second one shows that it has 1 item in it which is expected after that code. No problem there, is it in your third screenshot? Can you clarify/edit your question to make it clearer where the problem lies.
    – Beebee
    Sep 2, 2015 at 10:07
  • @Beebee I'm sorry, maybe I did not diagnose the problem correctly. The main thing is that the script is not included. Now, I assumed that it is because somehow the 'js' does not contain the proper element (In the second screenshot I can't see the string in the debugger that is there in the third).
    – Dalmaz
    Sep 2, 2015 at 10:43
  • It might very well be that I misunderstand the debugger/PHP workings, and the real cause of the script not working lies elsewhere.
    – Dalmaz
    Sep 2, 2015 at 10:52

1 Answer 1


I think your problem is you're trying to use a hook from a theme template.php? (Assuming because you have mytheme_page_alter as your function name for hook_page_alter). If you're not getting the JS included, it's specifically because you can't use hooks in template.php files. You need to create a new module.

  • Why does it works then if i use 'drupal_add_js()'?. Apparently, the code gets executed
    – Dalmaz
    Sep 2, 2015 at 11:01
  • 2
    I do not understand. mytheme_page_alter() gets executed with proper arguments. Besides, the drupal.org/node/1728096 says you can "Call hook_*_alter() functions which allow you to alter various parts of Drupal's internals, including the render elements in forms." in template.php?
    – Dalmaz
    Sep 2, 2015 at 11:30
  • 1
    " you can't use hooks in template.php files." - you can use some hooks, so please be more specific about such things.
    – Mołot
    Sep 2, 2015 at 12:37
  • 1
    Yep apologies, didn't know you could implement alter hooks. This answer is wrong.
    – Beebee
    Sep 2, 2015 at 12:59
  • 1
    @Dalmaz yes as you have shown, hook_page_alter CAN be implemented in template.php
    – Beebee
    Sep 2, 2015 at 12:59

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