| 8 system.api.php | hook_library_info() |
Registers JavaScript/CSS libraries associated with a module.
Modules implementing this return an array of arrays. The key to each sub-array is the machine readable name of the library. Each library may contain the following items:
- 'title': The human readable name of the library.
- 'website': The URL of the library's web site.
- 'version': A string specifying the version of the library; intentionally not a float because a version like "1.2.3" is not a valid float. Use PHP's version_compare() to compare different versions.
- 'js': An array of JavaScript elements; each element's key is used as $data argument, each element's value is used as $options array for drupal_add_js(). To add library-specific (not module-specific) JavaScript settings, the key may be skipped, the value must specify 'type' => 'setting', and the actual settings must be contained in a 'data' element of the value.
- 'css': Like 'js', an array of CSS elements passed to drupal_add_css().
- 'dependencies': An array of libraries that are required for a library. Each element is an array listing the module and name of another library. Note that all dependencies for each dependent library will also be added when this library is added.
Registered information for a library should contain re-usable data only. Module- or implementation-specific data and integration logic should be added separately.
Return value
An array defining libraries associated with a module.
See also
Related topics
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- block_library_info in core/
modules/ block/ block.module - Implements hook_library_info().
- book_library_info in core/
modules/ book/ book.module - Implements hook_library_info().
- ckeditor_library_info in core/
modules/ ckeditor/ ckeditor.module - Implements hook_library_info().
- color_library_info in core/
modules/ color/ color.module - Implements hook_library_info().
- comment_library_info in core/
modules/ comment/ comment.module - Implements hook_library_info().
- drupal_get_library in core/
includes/ common.inc - Retrieves information for a JavaScript/CSS library.
File
- core/
modules/ system/ system.api.php, line 370 - Hooks provided by Drupal core and the System module.
Code
function hook_library_info() {
// Library One.
$libraries['library-1'] = array(
'title' => 'Library One',
'website' => 'http://example.com/library-1',
'version' => '1.2',
'js' => array(
drupal_get_path('module', 'my_module') . '/library-1.js' => array(),
),
'css' => array(
drupal_get_path('module', 'my_module') . '/library-2.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
// Library Two.
$libraries['library-2'] = array(
'title' => 'Library Two',
'website' => 'http://example.com/library-2',
'version' => '3.1-beta1',
'js' => array(
// JavaScript settings may use the 'data' key.
array(
'type' => 'setting',
'data' => array('library2' => TRUE),
),
),
'dependencies' => array(
// Require jQuery UI core by System module.
array('system', 'jquery.ui.core'),
// Require our other library.
array('my_module', 'library-1'),
// Require another library.
array('other_module', 'library-3'),
),
);
return $libraries;
}
Comments
D7: hook_library()
PermalinkIn Drupal 7, this was called hook_library().
Naming conventions for libaries
PermalinkThe names of each library typically are name-spaced as such:
$libraries['name-of-library.sub-library']$libraries['name-of-library.library-category.thing-in-category']
Note that the name of the module is not always the same thing as the name of the library. Some good examples are in system_library_info() such as:
$libraries['jquery.ui.draggable']$libraries['jquery.effects.shake']
So to add these libraries via an #attached property (the recommended approach) would look like this:
$element['#attached']['library'] = array(array('system', 'jquery.ui.draggable')
);
Or via drupal_add_library():
drupal_add_library('system', 'jquery.ui.draggable');