drupal_add_library

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 drupal_add_library($module, $name, $every_page = NULL)

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.

Parameters

$module The name of the module that registered the library.

$name The name of the library to add.

$every_page Set to TRUE if this library is added to every page on the site. Only items with the every_page flag set to TRUE can participate in aggregation.

Return value

TRUE if the library was successfully added; FALSE if the library or one of its dependencies could not be added.

See also

drupal_get_library()

hook_library()

hook_library_alter()

▾ 13 functions call drupal_add_library()

ajax_example_render_link in examples/ajax_example/ajax_example_misc.inc
Demonstrates a clickable AJAX-enabled link using the 'use-ajax' class.
drupal_add_js in includes/common.inc
Adds a JavaScript file, setting, or inline code to the page.
drupal_add_tabledrag in includes/common.inc
Assist in adding the tableDrag JavaScript behavior to a themed table.
drupal_process_attached in includes/common.inc
Adds attachments to a render() structure.
JavaScriptTestCase::testLibraryAlter in modules/simpletest/tests/common.test
Adds a JavaScript library to the page and alters it.
JavaScriptTestCase::testLibraryRender in modules/simpletest/tests/common.test
Adds a library to the page and tests for both its JavaScript and its CSS.
JavaScriptTestCase::testLibraryUnknown in modules/simpletest/tests/common.test
Tests non-existing libraries.
overlay_set_mode in modules/overlay/overlay.module
Sets the overlay mode and adds proper JavaScript and styles to the page.
theme_textarea in includes/form.inc
Returns HTML for a textarea form element.
theme_textfield in includes/form.inc
Returns HTML for a textfield form element.
theme_vertical_tabs in includes/form.inc
Returns HTML for an element's children fieldsets as vertical tabs.
user_filter_form in modules/user/user.admin.inc
Form builder; Return form for user administration filters.
_batch_progress_page_js in includes/batch.inc
Output a batch processing page with JavaScript support.

Code

includes/common.inc, line 4506

<?php
function drupal_add_library($module, $name, $every_page = NULL) {
  $added = &drupal_static(__FUNCTION__, array());

  // Only process the library if it exists and it was not added already.
  if (!isset($added[$module][$name])) {
    if ($library = drupal_get_library($module, $name)) {
      // Add all components within the library.
      $elements['#attached'] = array(
        'library' => $library['dependencies'], 
        'js' => $library['js'], 
        'css' => $library['css'],
      );
      $added[$module][$name] = drupal_process_attached($elements, JS_LIBRARY, TRUE, $every_page);
    }
    else {
      // Requested library does not exist.
      $added[$module][$name] = FALSE;
    }
  }

  return $added[$module][$name];
}
?>