Hi guys

I was wondering if it is possible to load libraries via the #attached property from render arrays.

Cheers

Comments

jm.federico’s picture

Ah, and if so, if any one of you could provide an example. Thankx

Solthun’s picture

Not sure if this is what you meant exactly but this is an example that works:
http://api.drupal.org/api/drupal/includes!ajax.inc/group/ajax/7#comment-...

jm.federico’s picture

@Solthun

Thanks for the link, it works with libraries registered with Drupal Core library functionality, but can't get it to work with libraries registered with Library Api module.

Cheers

Jelle_S’s picture

Yes it's possible:

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

So, in short, just add a libraries_load key to your #attached array, with as value an array of parameters to pass to libraries_load().

jm.federico’s picture

Status: Active » Fixed

Thanks!!!!!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Jaypan’s picture

Issue summary: View changes

Note that this:

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

Should be this:

$form['myelement']['#attached']['libraries_load'][] = 'myAwesomeLibrary';
Jelle_S’s picture

@Jaypan:

No it should not ;-)

See drupal_process_attached():

// Add additional types of attachments specified in the render() structure.
// Libraries, JavaScript and CSS have been added already, as they require
// special handling.
foreach ($elements ['#attached'] as $callback => $options) {
  if (function_exists($callback)) {
    foreach ($elements ['#attached'][$callback] as $args) {
      call_user_func_array($callback, $args);
    }
  }
}

The second argument to call_user_func_array() should be an array :-).

Jaypan’s picture

Oh, you're right. I had an additional array layered in my code I was working on.