function hook_library_info_alter

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_alter()
  2. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_alter()
  3. 10 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_alter()

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, passed by reference. The array key for any particular library will be the name registered in *.libraries.yml. In the example below, the array key would be $libraries['foo'].


  foo:
    js:
      .......
  

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

5 functions implement hook_library_info_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

big_pipe_bypass_js_library_info_alter in core/modules/big_pipe/tests/modules/big_pipe_bypass_js/big_pipe_bypass_js.module
Implements hook_library_info_alter().
claro_system_module_invoked_library_info_alter in core/themes/claro/claro.theme
Called by system.module via its hook_library_info_alter().
jquery_keyevent_polyfill_test_library_info_alter in core/modules/system/tests/modules/jquery_keyevent_polyfill_test/jquery_keyevent_polyfill_test.module
Implements hook_library_info_alter().
test_theme_library_info_alter in core/modules/system/tests/themes/test_theme/test_theme.theme
Implements hook_library_info_alter().
unversioned_assets_test_library_info_alter in core/modules/system/tests/modules/unversioned_assets_test/unversioned_assets_test.module
Implements hook_library_info_build().
1 invocation of hook_library_info_alter()
LibraryDiscoveryParser::parseLibraryInfo in core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
Parses a given library file and allows modules and themes to alter it.

File

core/lib/Drupal/Core/Render/theme.api.php, line 1029

Code

function hook_library_info_alter(&$libraries, $extension) {
    // Update imaginary library 'foo' to version 2.0.
    if ($extension === 'core' && isset($libraries['foo'])) {
        // Verify existing version is older than the one we are updating to.
        if (version_compare($libraries['foo']['version'], '2.0', '<')) {
            // Update the existing 'foo' to version 2.0.
            $libraries['foo']['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/foo';
            // 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::service('extension.list.module')->getPath('foo_update') . '/js';
            $new_js = [];
            $replacements = [
                $old_path . '/foo.js' => $new_path . '/foo-2.0.js',
            ];
            foreach ($libraries['foo']['js'] as $source => $options) {
                if (isset($replacements[$source])) {
                    $new_js[$replacements[$source]] = $options;
                }
                else {
                    $new_js[$source] = $options;
                }
            }
            $libraries['foo']['js'] = $new_js;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.