You are here

function hook_library_info_alter in Drupal 9

Same name and namespace in other branches
  1. 8 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 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

11 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.

ckeditor_library_info_alter in core/modules/ckeditor/ckeditor.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().
color_library_info_alter in core/modules/color/color.module
Implements hook_library_info_alter().
common_test_library_info_alter in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_library_info_alter().
jquery_keyevent_polyfill_test_library_info_alter in core/modules/system/tests/modules/jquery_keyevent_polyfilll_test/jquery_keyevent_polyfill_test.module
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 990
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::service('extension.list.module')
        ->getPath('farbtastic_update') . '/js';
      $new_js = [];
      $replacements = [
        $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;
    }
  }
}