2

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

  • 1
    If you copy-pasted that code, you got two js indexes in that array. $page['#attached']['js']['js'] – Beebee Sep 2 '15 at 12:58
3

The code that finally got it included an important change with regard to @AyeshK 's answer. The [attached] is not put at the top level if $page , but inside ['content'] or ['header'].

The adapted code from the Format#1 is now:

function MODULE_page_alter(&$page) {
  $my_path = drupal_get_path('module', 'MODULE' );
  $page ['content']['#attached']['js'][] = array(
    'type'=>'file',
    'data' => $my_path . '/module_test.js'
  );
}  
2

Your code is almost correct. However, when you have a numeric key in [#attached][js], and its value is an array, you must include the data key.

Format #1

function MODULE_page_alter(&$page) {
  $my_path = drupal_get_path('module', 'MODULE' );
  $page ['#attached']['js'][] = array(
    'type'=>'file',
    'data' => $my_path . '/module_test.js'
  );
}

Format #2

function MODULE_page_alter(&$page) {
  $my_path = drupal_get_path('module', 'MODULE' );
  $page ['#attached']['js'] = array(
    $my_path . '/module_test.js'
  );
}

Format #

function MODULE_page_alter(&$page) {
  $my_path = drupal_get_path('module', 'MODULE' );
  $page ['#attached']['js'] = array(
    $my_path . '/module_test.js' => array(
      'type' => 'file',
    ),
  );
}
  • I do not get the errors now, but the js is not loaded.... – Dalmaz Sep 2 '15 at 13:34
  • 1
    @Dalmaz hope you disabled all aggregation/caching etc? If not, did you clear your caches after making any changes? Otherwise the changes might not get pulled through properly. If using drush you can "drush vset preprocess_js 0" to turn off javascript aggregation. Or just go to Config > development > performance – Beebee Sep 3 '15 at 14:42
  • @Beebee Cache was turned off, I was clearing the cache. Just to make sure, I deleted the contents of the files/js directory, and used the drush command you suggested. It still does not work, with drupal_add_js() pulling properly a sister file. – Dalmaz Sep 3 '15 at 20:00
  • just to make sure, I used the code in another theme (bartik) - I was using an omega 4 subtheme,so Ithought maybe there's something peculiar – Dalmaz Sep 3 '15 at 20:20
  • Got it finally. The [#attached] should be inside an element of a render array, with the left-hand side of the ascription being ['header']['attached']['js'] []=... Now the question is: why is it so? – Dalmaz Sep 3 '15 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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