hook_library_info_alter(&$libraries, $extension)
Alter libraries provided by an extension.
Allows modules and themes to change libraries' definitions; mostly used to update a library to a newer version, while ensuring backward compatibility. In general, such manipulations should only be done to extend the library's functionality in a backward-compatible way, to avoid breaking other modules and themes that may be using the library.
Parameters
array $libraries: An associative array of libraries registered by $extension. Keyed by internal library name and passed by reference.
string $extension: Can either be 'core' or the machine name of the extension that registered the libraries.
See also
\Drupal\Core\Asset\LibraryDiscoveryParser::parseLibraryInfo()
Related topics
- Hooks
- Define functions that alter the behavior of Drupal core.
File
- core/lib/Drupal/Core/Render/theme.api.php, line 952
- Hooks and documentation related to the theme and render system.
Code
function hook_library_info_alter(&$libraries, $extension) { // Update Farbtastic to version 2.0. if ($extension == 'core' && isset($libraries['jquery.farbtastic'])) { // Verify existing version is older than the one we are updating to. if (version_compare($libraries['jquery.farbtastic']['version'], '2.0', '<')) { // Update the existing Farbtastic to version 2.0. $libraries['jquery.farbtastic']['version'] = '2.0'; // To accurately replace library files, the order of files and the options // of each file have to be retained; e.g., like this: $old_path = 'assets/vendor/farbtastic'; // Since the replaced library files are no longer located in a directory // relative to the original extension, specify an absolute path (relative // to DRUPAL_ROOT / base_path()) to the new location. $new_path = '/' . drupal_get_path('module', 'farbtastic_update') . '/js'; $new_js = array(); $replacements = array( $old_path . '/farbtastic.js' => $new_path . '/farbtastic-2.0.js', ); foreach ($libraries['jquery.farbtastic']['js'] as $source => $options) { if (isset($replacements[$source])) { $new_js[$replacements[$source]] = $options; } else { $new_js[$source] = $options; } } $libraries['jquery.farbtastic']['js'] = $new_js; } } }
Please login to continue.