You are here

function libraries_load in Libraries API 8.3

Same name and namespace in other branches
  1. 7.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.

Deprecated

Will be removed before a stable Drupal 8 release. Please use the new library load and managment concepts described at: https://www.drupal.org/node/2170763

2 calls to libraries_load()
LibrariesWebTest::testCallbacks in src/Tests/LibrariesWebTest.php
Tests the applying of callbacks.
LibrariesWebTest::_testLibrariesLoad in src/Tests/LibrariesWebTest.php
Tests libraries_load().
2 string references to 'libraries_load'
LibrariesWebTest::testCallbacks in src/Tests/LibrariesWebTest.php
Tests the applying of callbacks.
LibrariesWebTest::_testLibrariesLoad in src/Tests/LibrariesWebTest.php
Tests libraries_load().

File

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

Code

function libraries_load($name, $variant = NULL) {
  $loaded =& drupal_static(__FUNCTION__, []);
  if (!isset($loaded[$name])) {
    $library = \Drupal::cache('libraries')
      ->get($name);
    if ($library) {
      $library = $library->data;
    }
    else {
      $library = libraries_detect($name);
      \Drupal::cache('libraries')
        ->set($name, $library);
    }

    // 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'] += [
        $variant => [
          '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']);

    // 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];
}