You are here

function hook_library_info_alter in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_alter()
  2. 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 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

8 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().
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().
locale_library_info_alter in core/modules/locale/locale.module
Implements hook_library_info_alter().
quickedit_library_info_alter in core/modules/quickedit/quickedit.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 981
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 = [];
      $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;
    }
  }
}