You are here

function libraries_load in Libraries API 7.3

Same name and namespace in other branches
  1. 8.3 libraries.module \libraries_load()
  2. 7.2 libraries.module \libraries_load()

Loads a library.

Parameters

$name: The name of the library to load.

$variant: The name of the variant to load. Note that only one variant of a library can be loaded within a single request. The variant that has been passed first is used; different variant names in subsequent calls are ignored.

Return value

An associative array of the library information as returned from libraries_info(). The top-level properties contain the effective definition of the library (variant) that has been loaded. Additionally:

  • installed: Whether the library is installed, as determined by libraries_detect_library().
  • loaded: Either the amount of library files that have been loaded, or FALSE if the library could not be loaded.

See hook_libraries_info() for more information.

3 calls to libraries_load()
LibrariesTestCase::testCallbacks in tests/libraries.test
Tests the applying of callbacks.
LibrariesTestCase::testLibrariesLoad in tests/libraries.test
Tests libraries_load().
_libraries_test_module_load in tests/modules/libraries_test_module/libraries_test_module.module
Loads a specified library (variant) for testing.
2 string references to 'libraries_load'
LibrariesTestCase::testCallbacks in tests/libraries.test
Tests the applying of callbacks.
LibrariesTestCase::testLibrariesLoad in tests/libraries.test
Tests libraries_load().

File

./libraries.module, line 672
External library handling for Drupal modules.

Code

function libraries_load($name, $variant = NULL) {
  $loaded =& drupal_static(__FUNCTION__, array());
  if (!isset($loaded[$name])) {
    $library = cache_get($name, 'cache_libraries');
    if ($library) {
      $library = $library->data;
    }
    else {
      $library = libraries_detect($name);
      cache_set($name, $library, 'cache_libraries');
    }

    // Exit early if the library was not found.
    if ($library === FALSE) {
      $loaded[$name] = $library;
      return $loaded[$name];
    }

    // If a variant was specified, override the top-level properties with the
    // variant properties.
    if (isset($variant)) {

      // Ensure that the $variant key exists, and if it does not, set its
      // 'installed' property to FALSE by default. This will prevent the loading
      // of the library files below.
      $library['variants'] += array(
        $variant => array(
          'installed' => FALSE,
        ),
      );
      $library = array_merge($library, $library['variants'][$variant]);
    }

    // Regardless of whether a specific variant was requested or not, there can
    // only be one variant of a library within a single request.
    unset($library['variants']);

    // Invoke callbacks in the 'pre-dependencies-load' group.
    libraries_invoke('pre-dependencies-load', $library);

    // If the library (variant) is installed, load it.
    $library['loaded'] = FALSE;
    if ($library['installed']) {

      // Load library dependencies.
      if (isset($library['dependencies'])) {
        foreach ($library['dependencies'] as $dependency) {
          libraries_load($dependency);
        }
      }

      // Invoke callbacks in the 'pre-load' group.
      libraries_invoke('pre-load', $library);

      // Load all the files associated with the library.
      $library['loaded'] = libraries_load_files($library);

      // Invoke callbacks in the 'post-load' group.
      libraries_invoke('post-load', $library);
    }
    $loaded[$name] = $library;
  }
  return $loaded[$name];
}