0

I have seen something like the following document for forms:

$form['myelement']['#attached']['libraries_load'][] = array('myAwesomeLibrary');

However, this does not work in render arrays for pages or blocks. At least, not in Drupal 7. When attaching a JS library to a render array in a page or block, should I use the 'libraries_load' or 'library' key within the '#attached' array? Neither seem to work.

Of course, it's possible to use the 'js' key along with libraries_get_path, but I'm not sure that's best practice for Drupal 7, and it doesn't really take advantage of the path in hook_libraries_info.

4
  • Ah, I missed that last bit when typing up my answer. What do you mean by "it doesn't really take advantage of the path in hook_libraries_info"? Aug 23, 2015 at 21:20
  • Annnd answer deleted. I need more sleep. :-/ Aug 24, 2015 at 16:39
  • 3
    possible duplicate of How do I add javascript to a node or block
    – leymannx
    Aug 24, 2015 at 16:41
  • Question about Library/Libraries API seems different than a generic question about adding JS...
    – Beebee
    Aug 25, 2015 at 9:45

2 Answers 2

3

This might be a simple case of misunderstanding. The difference between libraries_load and library #attached keys can be confusing.

['#attached']['libraries_load'] is for libraries defined through hook_libraries_info which is in turn a part of the Libraries API module. If a module has defined a hook hook_libraries_info but your installation doesn't have the Libraries API module installed, then that library will never load. The libraries_load key of #attached will not fire for the same reason. Ideally though, this module with the hook_libraries_info should have a dependency on the Libraries API module if it was written correctly.

['#attached']['library'] is for libraries defined through drupal core library api hook_library. This should always work without any additional modules, provided you've defined a hook_library in your module (and not hook_libraries_info)

Hope this helps you get a clearer idea of what's exactly wrong. You can't have these interchanging, and just trying libraries_load or library randomly to see if they work. Both require a certain prerequisite (see above) to function correctly.

2
  • If you answer questions marked "possible duplicate", please also comment under question and tell why you don't think it's a duplicate. It's not a rule it requirement, but if you have a reason, sharing it may help to keep question open.
    – Mołot
    Aug 24, 2015 at 20:45
  • There's nothing about libraries in that Q&A
    – Beebee
    Aug 25, 2015 at 9:44
0

There are two ways to do this that come to my mind. The first is registering a hook_library() within a custom module. This will allow you to include multiple JS/CSS files.

However, a more common approach would be:

BLOCK: drupal_add_js() or drupal_add_css() in the hook_block_view() implementation.

PAGE: If this is a page you are defining through code, you could use the BLOCK implementation above or you could do a template_preprocess_page().

hook_library: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_library/7

drupal_add_js: https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7

drupal_add_css: https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_css/7

template_preprocess_page: https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7

Your Answer

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

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