Drupal allows modules to add dynamic library definitions by implementing hook_library_info_build()
. Drupal core doesn't implement this hook, except in a test module.
function common_test_library_info_build() {
$libraries = [];
if (\Drupal::state()->get('common_test.library_info_build_test')) {
$libraries['dynamic_library'] = [
'version' => '1.0',
'css' => [
'base' => [
'common_test.css' => [],
],
],
];
}
return $libraries;
}
If you instead need to alter the library definitions given from another module, hook_library_info_alter()
is the correct hook to use.
function locale_library_info_alter(array &$libraries, $module) {
if ($module === 'core' && isset($libraries['jquery.ui.datepicker'])) {
$libraries['jquery.ui.datepicker']['dependencies'][] = 'locale/drupal.locale.datepicker';
$libraries['jquery.ui.datepicker']['drupalSettings']['jquery']['ui']['datepicker'] = [
'isRTL' => NULL,
'firstDay' => NULL,
];
}
// When the locale module is enabled, we update the core/drupal library to
// have a dependency on the locale/translations library, which provides
// window.drupalTranslations, containing the translations for all strings in
// JavaScript assets in the current language.
// @see locale_js_alter()
if ($module === 'core' && isset($libraries['drupal'])) {
$libraries['drupal']['dependencies'][] = 'locale/translations';
}
}
*.library.yml
needs to be called *.libraries.yml