/**
* Adds multiple JavaScript or CSS files at the same time.
*
* A library defines a set of JavaScript and/or CSS files, optionally using
* settings, and optionally requiring another library. For example, a library
* can be a jQuery plugin, a JavaScript framework, or a CSS framework. This
* function allows modules to load a library defined/shipped by itself or a
* depending module; without having to add all files of the library separately.
* Each library is only loaded once.
*
* @param $module
* The name of the module that registered the library.
* @param $name
* The name of the library to add.
* @return
* TRUE when the library was successfully added or FALSE if the library or one
* of its dependencies could not be added.
*
* @see drupal_get_library()
* @see hook_library()
* @see hook_library_alter()
*/
function drupal_add_library($module, $name) {
$added = &drupal_static(__FUNCTION__, array());
// Only process the library if it exists and it hasn't been added yet.
if (!isset($added[$module][$name]) && $library = drupal_get_library($module, $name)) {
// State that we are processing the library, so that it's not processed again.
$added[$module][$name] = TRUE;
// Prepare the library for addition.
$library += array('dependencies' => array(), 'js' => array(), 'css' => array());
// Ensure the dependencies are available first.
foreach ($library['dependencies'] as $dependency) {
// Directly invoke drupal_add_library() to test its return value. The first
// parameter is the library's hosting module, while the second argument is
// the library's name.
if (drupal_add_library($dependency[0], $dependency[1]) === FALSE) {
// If any dependent library could not be added, this library will
// break, so stop here.
return $added[$module][$name] = FALSE;
}
}
// Add related JavaScript.
foreach ($library['js'] as $data => $options) {
// For JS settings we need to transform $options['data'] into $data.
if (isset($options['type'], $options['data']) && $options['type'] == 'setting') {
$data = $options['data'];
unset($options['data']);
}
// All JavaScript contained within the libraries are given a default
// weight of JS_LIBRARY.
elseif (!isset($options['weight'])) {
$options['weight'] = JS_LIBRARY;
}
drupal_add_js($data, $options);
}
// Finally, add the stylesheets.
foreach ($library['css'] as $data => $options) {
drupal_add_js($data, $options);
}
}
return $added[$module][$name];
}