Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By tadityar on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0.0-beta4
Issue links:
Description:
There was no way to dynamically register libraries. Using hook_library_info_alter()
to register new library is possible but doesn't end well in alter/weight.
Therefore a new hook is created namely hook_library_info_build()
D8
/**
* Add dynamic library definitions.
*
* Modules may implement this hook to add dynamic library definitions. Static
* libraries, which do not depend on any runtime information, should be declared
* in a modulename.libraries.yml file instead.
*
* @return array[]
* An array of library definitions to register, keyed by library ID. The
* library ID will be prefixed with the module name automatically.
*
* @see core.libraries.yml
* @see hook_library_info_alter()
*/
function hook_library_info_build() {
$libraries = [];
// Add a library whose information changes depending on certain conditions.
$libraries['mymodule.zombie'] = [
'dependencies' => [
'core/backbone',
],
];
if (Drupal::moduleHandler()->moduleExists('minifyzombies')) {
$libraries['mymodule.zombie'] += [
'js' => [
'mymodule.zombie.min.js' => [],
],
'css' => [
'base' => [
'mymodule.zombie.min.css' => [],
],
],
];
}
else {
$libraries['mymodule.zombie'] += [
'js' => [
'mymodule.zombie.js' => [],
],
'css' => [
'base' => [
'mymodule.zombie.css' => [],
],
],
];
}
// Add a library only if a certain condition is met. If code wants to
// integrate with this library it is safe to (try to) load it unconditionally
// without reproducing this check. If the library definition does not exist
// the library (of course) not be loaded but no notices or errors will be
// triggered.
if (Drupal::moduleHandler()->moduleExists('vampirize')) {
$libraries['mymodule.vampire'] = [
'js' => [
'js/vampire.js' => [],
],
'css' => [
'base' => [
'css/vampire.css',
],
],
'dependencies' => [
'core/jquery',
],
];
}
return $libraries;
}
Impacts:
Module developers
Comments
There is a confusion on the
There is a confusion on the way to include library to render array as described in the above example
$build['the_element_that_needs_the_asset_library']['#attached']['library'][] = 'mymodule/mymodule.vampire';
"mymodule" namespace is required for including libraries in D8.
There is an issue to clarify the docs about including dynamic library - https://www.drupal.org/project/drupal/issues/2690991
the library file needs to have an array of options
Not sure if it will help someone, but for the library to work it needs to have an array of options like weight or just an empty array
something line this
just like it is declared in the yaml file
Note the brackets in the yaml
Library id auto prefixed with module name
As stated in hook_library_info_alter():
Maybe helpful to note once more in case anyone is wondering why the library won't add.