1

This question is specific to D7: I'm trying to load in jQuery UI modal into the theme. I've put the following in my THEMENAME_preprocess_html:

$variables['page']['content']['#attached']['library'][] = array('system', 'ui.dialog');

This isn't loading the library at all.

DPM-ing the $variables array after the above line shows that it's all added correctly (There's even other stuff in there like, addthis.js): DPM of variables array

Direct link: https://i.sstatic.net/6iC3h.png

Obviously all caches were cleared. At this point the console shows an error that dialog is not defined. But as soon as I add this (in the same function, just below the #attached line):

drupal_add_library('system', 'ui.dialog');

then the dialog modal opens and starts playing nicely. Am I doing something wrong with #attached? I'm really puzzled as to why one's working and the other doesn't. Ideally I'd like to use #attached in the cases where the variables array gets cached and the preprocess doesn't get called.

1 Answer 1

3

Because the render array has already been rendered before the preprocess functions, '#attached' is not supported in preprocess functions until Drupal 8.

The issue where it was added is https://www.drupal.org/node/2346369

The related Drupal 8 change record is https://www.drupal.org/node/2352319 - this also shows the supported way of doing it in Drupal 7.

Using drupal_add_library() will work, but it will not be cached, so you will take a performance hit. You are better off adding it to the #attached array at a different step in the process.

4
  • Excellent. Thank you, so I guess the only way to go is to use drupal_add_library. Will this have issues when caching is turned on?
    – Beebee
    Commented Jul 11, 2017 at 15:20
  • 1
    I know the use of drupal_add_css & drupal_add_js are affected by caching but I think maybe it depends where you are using it (using it in blocks for example doesn't work properly when cached). I haven't looked into it enough to understand entirely but you will find out with a test run. Also, you could move your code, unless it really has to be in preprocess html. It could maybe go in hook_page_build() or hook_page_alter() instead.
    – rooby
    Commented Jul 11, 2017 at 15:33
  • yeah I don't know why I stuck with preprocess. Thank you for the clarification.
    – Beebee
    Commented Jul 11, 2017 at 15:40
  • 2
    "For whatever reason (maybe just oversight), '#attached' is not supported in preprocess functions until Drupal 8." <---- This is because the render array has already been rendered before the preprocess functions. Using drupal_add_library() will work, but it will not be cached, so you will take a performance hit. You are better off adding it to the #attached array at a different step in the process.
    – Jaypan
    Commented Jul 11, 2017 at 22:10

Your Answer

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

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