function hook_library_info_alter

Same name and namespace 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

12 functions implement hook_library_info_alter()

Note: the procedural functions in this list are found by pattern matching, so the list may include some functions that are not actually implementations of this hook.

BigPipeBypassJsHooks::libraryInfoAlter in core/modules/big_pipe/tests/modules/big_pipe_bypass_js/src/Hook/BigPipeBypassJsHooks.php
Implements hook_library_info_alter().
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().
Ckeditor5Hooks::libraryInfoAlter in core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php
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().
CommonTestThemeHooks::libraryInfoAlter in core/modules/system/tests/modules/common_test/src/Hook/CommonTestThemeHooks.php
Implements hook_library_info_alter().

... See full list

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 1034

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.