function hook_library_info_build

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_build()
  2. 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_build()
  3. 10 core/lib/Drupal/Core/Render/theme.api.php \hook_library_info_build()

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 value

array[] An array of library definitions to register, keyed by library ID. The library ID will be prefixed with the module name automatically.

See also

core.libraries.yml

hook_library_info_alter()

Related topics

2 functions implement hook_library_info_build()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

many_assets_test_library_info_build in core/modules/system/tests/modules/many_assets_test/many_assets_test.module
Implements hook_library_info_build().
sdc_library_info_build in core/modules/sdc/sdc.module
Implements hook_library_info_build().

File

core/lib/Drupal/Core/Render/theme.api.php, line 907

Code

function hook_library_info_build() : array {
    $libraries = [];
    // Add a library whose information changes depending on certain conditions.
    $libraries['zombie'] = [
        'dependencies' => [
            'core/once',
        ],
    ];
    if (Drupal::moduleHandler()->moduleExists('minifyzombies')) {
        $libraries['zombie'] += [
            'js' => [
                'zombie.min.js' => [],
            ],
            'css' => [
                'base' => [
                    'zombie.min.css' => [],
                ],
            ],
        ];
    }
    else {
        $libraries['zombie'] += [
            'js' => [
                'zombie.js' => [],
            ],
            'css' => [
                'base' => [
                    '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['vampire'] = [
            'js' => [
                'js/vampire.js' => [],
            ],
            'css' => [
                'base' => [
                    'css/vampire.css',
                ],
            ],
            'dependencies' => [
                'core/jquery',
            ],
        ];
    }
    return $libraries;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.