function drupal_get_library

Retrieves information for a JavaScript/CSS library.

Library information is statically cached. Libraries are keyed by module for several reasons:

  • Libraries are not unique. Multiple modules might ship with the same library in a different version or variant. This registry cannot (and does not attempt to) prevent library conflicts.
  • Modules implementing and thereby depending on a library that is registered by another module can only rely on that module's library.
  • Two (or more) modules can still register the same library and use it without conflicts in case the libraries are loaded on certain pages only.

@todo The purpose of drupal_get_*() is completely different to other page requisite API functions; find and use a different name.

Parameters

$module: The name of a module that registered a library.

$name: (optional) The name of a registered library to retrieve. By default, all libraries registered by $module are returned.

Return value

The definition of the requested library, if $name was passed and it exists, or FALSE if it does not exist. If no $name was passed, an associative array of libraries registered by $module is returned (which may be empty).

See also

drupal_add_library()

hook_library_info()

hook_library_info_alter()

5 calls to drupal_get_library()
drupal_add_library in drupal/core/includes/common.inc
Adds multiple JavaScript or CSS files at the same time.
JavaScriptTest::testGetLibrary in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests retrieval of libraries via drupal_get_library().
JavaScriptTest::testLibraryAlter in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Adds a JavaScript library to the page and alters it.
JavaScriptTest::testLibraryNameConflicts in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests that multiple modules can implement the same library.
JavaScriptTest::testLibraryUnknown in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests non-existing libraries.
1 string reference to 'drupal_get_library'
JavaScriptTest::testLibraryUnknown in drupal/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php
Tests non-existing libraries.

File

drupal/core/includes/common.inc, line 4440
Common functions that many Drupal modules will need to reference.

Code

function drupal_get_library($module, $name = NULL) {
  $libraries =& drupal_static(__FUNCTION__, array());
  if (!isset($libraries[$module])) {

    // Retrieve all libraries associated with the module.
    $module_libraries = module_invoke($module, 'library_info');
    if (empty($module_libraries)) {
      $module_libraries = array();
    }

    // Allow modules to alter the module's registered libraries.
    drupal_alter('library_info', $module_libraries, $module);
    foreach ($module_libraries as $key => $data) {
      if (is_array($data)) {

        // Add default elements to allow for easier processing.
        $module_libraries[$key] += array(
          'dependencies' => array(),
          'js' => array(),
          'css' => array(),
        );
        foreach ($module_libraries[$key]['js'] as $file => $options) {
          $module_libraries[$key]['js'][$file]['version'] = $module_libraries[$key]['version'];
        }
      }
    }
    $libraries[$module] = $module_libraries;
  }
  if (isset($name)) {
    if (!isset($libraries[$module][$name])) {
      $libraries[$module][$name] = FALSE;
    }
    return $libraries[$module][$name];
  }
  return $libraries[$module];
}