I tried to add js
to pages by means of #attached
, as it seems to be the recommended way. From what I understand, this involves altering a render array. hook_page_alter
seems to me like good candidate.
As per https://www.drupal.org/node/756722 (and answers below), I came with:
function MODULE_page_alter(&$page) {
$my_path = drupal_get_path('module', 'MODULE' );
$page ['#attached']['js'][] = array(
'js' => array(
'type'=>'file',
'data' => $my_path . '/module_test.js'),
);
}
The file does not get attached, though. If I use
drupal_add_js($my_path . '/module_test.js');
it does get attached (it's not a problem of the js file, access persmissions, etc.).
What is wrong?
PS. I asked a related, but apparently wrong-headed question here
js
indexes in that array.$page['#attached']['js']['js']