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 :
After $page['content']['#attached']['js'][] = array($theme_path . '/test.js');
, however, the array values are not set:
Still the value of array($theme_path . '/test.js')
is what it 'should' be:
It's as if the ascription did not go through.
What did I do wrong?
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.